我正在使用 UIDocument 访问核心数据并尝试在后台线程中设置 NSFetchedResultsController 以不阻塞主 UI。如果我使用 uidocument 的 managedobjectcontext 而不是孩子,它会起作用。我在堆栈溢出时从此处的答案之一中获取了其中的一些代码,但无法使其与 fetchedresultscontroller 一起使用。
[ZH peopleDocumentusingBlock:^(UIManagedDocument *peopleDoc){
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
context.parentContext = peopleDoc.managedObjectContext;
[context performBlock:^{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]];
[NSFetchedResultsController deleteCacheWithName:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageurl.length>0"];
request.predicate = predicate;
NSError *error = nil;
if(!self.totalNumberOfPeople) //set total number of people for this fetch used for show more records
{
self.totalNumberOfPeople = [context countForFetchRequest:request error:&error];
NSLog(@"total number of people -> %d",self.totalNumberOfPeople);
}
self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:@"peopleCache"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate refreshPeoplelTableview];
NSLog(@"fetched objects -> %d",[self.fetchedResultsController.fetchedObjects count]);
NSLog(@"total number of people -> %d",self.totalNumberOfPeople);
});
}];
}];
实际上context countforfetchrequest
会但不是 fetcheresultscontroller。
提前致谢!