1、查询对象,添加到数据源中
// 1.
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
request.sortDescriptors = @[sort];
// 执行查询请求
NSError *error = nil;
NSArray *result = [self.myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
[self.dataSource addObjectsFromArray:result];
2、添加对象
- (IBAction)insertModel:(UIBarButtonItem *)sender {
// 创建实体描述对象
NSEntityDescription *description = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:_myDelegate.persistentContainer.viewContext];
// 创建实体模型
Person *person = [[Person alloc] initWithEntity:description insertIntoManagedObjectContext:_myDelegate.persistentContainer.viewContext];
person.name = @"张三";
person.age = arc4random() % 100 + 1;
person.sex = @"女";
// 插入数据源数组
[self.dataSource addObject:person];
// 刷新
NSIndexPath *index = [NSIndexPath indexPathForRow:_dataSource.count - 1 inSection:0];
[self.myTableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
// 保存在数据管理器
[_myDelegate saveContext];
}
3.删除对象
Person *person = self.dataSource[indexPath.row];
[_myDelegate.persistentContainer.viewContext deleteObject:person];
[_myDelegate saveContext];
[self.dataSource removeObject:person];
[self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
4.修改对象
Person *person = self.dataSource[indexPath.row];
person.name = @"李四";
[self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.myDelegate saveContext];
下面内容是多线程开发部分
1.ManagedObjectContext的类型
NSConfinementConcurrencyType - context使用thread confinement 模式
NSPrivateQueueConcurrencyType - context在私有线程上的
NSMainQueueConcurrencyType - context在主线程上
2.并行的解决方案之Notification
简单来说,就是不同的线程使用不同的context进行操作,当一个线程的context发生变化后,利用notification来通知另一个线程Context,另一个线程调用mergeChangesFromContextDidSaveNotification来合并变化。
- Notification的种类
NSManagedObjectContextObjectsDidChangeNotification 当Context中的变量改变时候触发。
NSManagedObjectContextDidSaveNotification 在一个context调用save完成以后触发。注意,这些managed object只能在当前线程使用,如果在另一个线程响应通知,要调用mergeChangesFromContextDidSaveNotification来合并变化。
NSManagedObjectContextWillSaveNotification。将要save。
一种比较好的iOS模式就是使用一个NSPersistentStoreCoordinator,以及两个独立的Contexts,一个context负责主线程与UI协作,一个context在后台负责耗时的处理。
例如:
创建子线程context
self.backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[self.backgroundContext setPersistentStoreCoordinator:self.mainContext.persistentStoreCoordinator];
// iOS 10以后才可以使用此方法
NSManagedObjectContext *backgroundContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).persistentContainer.newBackgroundContext;
self.backgroundContext = backgroundContext;
// 添加后台线程的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveContextSave:) name:NSManagedObjectContextDidSaveNotification object:self.backgroundContext];
[self.backgroundContext performBlock:^{
// 调用save会调用通知方法receiveContextSave合并改变
if ([self.backgroundContext hasChanges]) {
[self.backgroundContext save:nil];
}
}];
- (void)receiveContextSave:(NSNotification *)note {
[self.mainContext mergeChangesFromContextDidSaveNotification:note];
}
网友评论