.h
#import <CoreData/CoreData.h>
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory; // nice to have to reference files for core data
--------------------
.m
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (void)saveContext{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (NSManagedObjectContext *)managedObjectContext{
if (_managedObjectContext != nil) {
return_managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [selfpersistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContextalloc] init];
[_managedObjectContextsetPersistentStoreCoordinator:coordinator];
}
return_managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel{
if (_managedObjectModel != nil) {
return_managedObjectModel;
}
NSURL *modelURL = [[NSBundlemainBundle] URLForResource:@"Rank"withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModelalloc] initWithContentsOfURL:modelURL];
return_managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return_persistentStoreCoordinator;
}
NSURL *storeURL = [[selfapplicationDocumentsDirectory] URLByAppendingPathComponent:@"Rank.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:[selfmanagedObjectModel]];
if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:storeURL options:nilerror:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return_persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory{
return [[[NSFileManagerdefaultManager] URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask] lastObject];
}
---------------------
.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface DatabaseManager : NSObject
+ (id)sharedInstance;
-(void)insertWithName:(NSString*)name withScore:(int)score;
-(NSArray*)load;
@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
@end
----------------
.m
#import "DatabaseManager.h"
staticDatabaseManager* instance;
@implementation DatabaseManager
+ (id)sharedInstance {
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [selfnew];
});
returninstance;
}
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext* context = nil;
id delegate = [[UIApplicationsharedApplication] delegate];
if([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
-(void)insertWithName:(NSString*)name withScore:(int)score{
NSManagedObjectContext* context = [selfmanagedObjectContext];
NSManagedObject* entity = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Entity"inManagedObjectContext:context];
[entity setValue:name forKey:@"name"];
[entity setValue:[NSNumbernumberWithInt:score] forKey:@"score"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
-(NSArray*)load{
NSManagedObjectContext* context = [selfmanagedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequestalloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity"inManagedObjectContext:context];
NSSortDescriptor *sortByScore = [NSSortDescriptorsortDescriptorWithKey:@"score"ascending:NO];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:@[sortByScore]];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
-------------------
留言列表