core data 网上说不好用,不过还是有必要学习下,毕竟是苹果官方的。
1、创建项目,在项目中勾选use core data;
此时会在程序的AppDelegate 文件中生成一些core data 操作文件。
2、打开.xcdatamodeld文件;选择Add Entity,添加数据库表;选择Add Attribute,添加数据库的列;
3、添加数据库模型;选择New File->core data->NSManageObject subclass->勾选需要生成的模型,程序中就添加了UserInfo的两个文件。
4、然后就可以使用core data 代码;查询;删除;添加等操作;
一、查询
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return_fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[[NSFetchRequestalloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"Event"inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptoralloc] initWithKey:@"timeStamp"ascending:NO] autorelease];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsControlleralloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContextsectionNameKeyPath:nilcacheName:@"Master"] autorelease];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsControllerperformFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return_fetchedResultsController;
}
二、插入
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsControllermanagedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsControllerfetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescriptioninsertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}三、删除
NSManagedObjectContext *context = [self.fetchedResultsControllermanagedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}