@interface ViewController : UIViewController @property (nonatomic, retain) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator; @end
根据前文讨论的顺序,我们首先创建managedObjectModel(在那之前需要引入CoreData Framework):
- (NSManagedObjectModel *)managedObjectModel { if (nil != _managedObjectModel) { return _managedObjectModel; } _managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; return _managedObjectModel; }
- (NSManagedObjectContext *)managedObjectContext { if (nil != _managedObjectContext) { return _managedObjectContext; } _managedObjectContext = [[NSManagedObjectContext alloc] init]; return _managedObjectContext; }
#pragma mark - #pragma mark - Core Data Stack - (NSManagedObjectModel *)managedObjectModel { if (nil != _managedObjectModel) { return _managedObjectModel; } _managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; return _managedObjectModel; } - (NSManagedObjectContext *)managedObjectContext { if (nil != _managedObjectContext) { return _managedObjectContext; } _managedObjectContext = [[NSManagedObjectContext alloc] init]; if (self.persistentStoreCoordinator) { [_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; } return _managedObjectContext; } - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (nil != _persistentStoreCoordinator) { return _persistentStoreCoordinator; } NSString *storeType = NSSQLiteStoreType; NSString *storeName = @"cdNBA.sqlite"; NSError *error = NULL; NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:storeName]]; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; if (![_persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nilURL:storeURL options:nil error:&error]) { NSLog(@"Error : %@\n", [error localizedDescription]); NSAssert1(YES, @"Failed to create store %@ with NSSQLiteStoreType", [storeURL path]); } return _persistentStoreCoordinator; } #pragma mark - #pragma mark Application's Documents Directory - (NSString *)applicationDocumentsDirectory { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return basePath; }
在搭建完基础的Core Data栈结构后,就可以写一些简单的CURD代码了:
#pragma mark - #pragma mark - CURD Operations - (BOOL)createTeamWithName:(NSString *)teamName city:(NSString *)teamCity { if (!teamName || !teamCity) { return NO; } NSManagedObject *teamObject = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext]; [teamObject setValue:teamName forKey:@"name"]; [teamObject setValue:teamCity forKey:@"city"]; return YES; } - (NSArray *)fetchTeamList { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:[NSEntityDescription entityForName:@"Team" inManagedObjectContext:self.managedObjectContext]]; NSError *error = NULL; NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (error) { NSLog(@"Error : %@\n", [error localizedDescription]); } [fetchRequest release], fetchRequest = nil; return array; }
我们可以把读写的代码放到viewDidLoad函数中:
- (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self createTeamWithName:@"Heat"city:@"Miami"]; [self createTeamWithName:@"Lakers"city:@"LA"]; [self saveContext]; NSArray *teamArray = [self fetchTeamList]; if (teamArray) { for (NSManagedObject *teamObject in teamArray) { NSString *teamName = [teamObject valueForKey:@"name"]; NSString *teamCity = [teamObject valueForKey:@"city"]; NSLog(@"Team info : %@, %@\n", teamName, teamCity); } } }
由于在context中创建的对象只是存在于内存中,所以我们还需要显式地把数据保存到sqlite文件里:
#pragma mark - #pragma mark - Save Context - (void)saveContext { NSError *error = NULL; NSManagedObjectContext *moc = self.managedObjectContext; if (moc && [moc hasChanges] && ![moc save:&error]) { NSLog(@"Error %@, %@", error, [error localizedDescription]); abort(); } }
2013-01-16 14:32:52.638 cdNBA[4717:c07] Team info : Heat, Miami 2013-01-16 14:32:52.639 cdNBA[4717:c07] Team info : Lakers, LA