关于Core Data的一些整理(四)

  • 调用Core Data文件中的Request模板进行条件匹配

关于Core Data的一些整理(四)-LMLPHP

 //获取ObjectModel相当于获取Core Date文件
NSManagedObjectModel *model = self.coreDataStack.contenxt.persistentStoreCoordinator.managedObjectModel;
self.fetchRequest = [model fetchRequestTemplateForName:@"FetchRequest"];
self.venues = [self.coreDataStack.contenxt executeFetchRequest:self.fetchRequest error:nil];
  • iOS8新增Asynchronous异步调用Core Data,提高性能
 @property (nonatomic, strong) NSFetchRequest *fetchRequest;
@property (nonatomic, strong) NSAsynchronousFetchRequest *asyncFetchRequest; //asyncFetch只是对普通request的封装
self.fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Venue"];
self.asyncFetchRequest = [[NSAsynchronousFetchRequest alloc] initWithFetchRequest:self.fetchRequest completionBlock:^(NSAsynchronousFetchResult * _Nonnull result) {
self.venues = result.finalResult;
[self.tableView reloadData];
}];
//注意调用的是executeRequest:方法
[self.coreDataStack.contenxt executeRequest:self.asyncFetchRequest error:nil];
  • iOS8新增批(Batch)处理,链接

Batch Updates或Batch Deletes与普通NSFetchRequest请求不同的是,他们都基于NSPersistentStoreRequest类,这是直接对底层的NSPersistentStore进行数据操作的请求,而不经过NSManagedObjectContext,因此避免将固态存储的数据转移到内存再处理的过程,因此效率大大提升。

 //设置批处理请求,包含更新的内容,影响的stores
NSBatchUpdateRequest *batchUpdate = [NSBatchUpdateRequest batchUpdateRequestWithEntityName:@"Venue"];
batchUpdate.propertiesToUpdate = @{@"favorite":@YES};
batchUpdate.affectedStores = self.coreDataStack.contenxt.persistentStoreCoordinator.persistentStores;
batchUpdate.resultType = NSUpdatedObjectsCountResultType;//返回更新的行数 NSBatchUpdateResult *batchResult = [self.coreDataStack.contenxt executeRequest:batchUpdate error:nil];
NSLog(@"%@", batchResult.result);
//在iOS9中又新增
04-30 21:15