Z S*_*Z S 2 core-data grand-central-dispatch nsmanagedobject nsmanagedobjectcontext ios
我需要一些帮助来使用Core Data with GCD中的对象; 我似乎得到NSManagedObjects,即使我访问他们的属性,也没有出现在主线程中.会感激一些帮助.
这就是我正在做的事情:在启动时,我需要从Core Data DB加载一个Person列表,在后台进行一些自定义处理,然后重新加载表以显示名称.我通过仅将objectID传递到GCD队列来遵循Core Data多线程的指导原则.但是当我在主线程上重新加载tableview时,我从未看到为联系人显示的名称(或其他属性),并且仔细观察,NSManagedObjects结果是主线程上的错误,即使我访问了各种属性的cellForRowAtIndexPath.当我NSLog时,name属性在后台线程中可见; 它也在cellForRowAtIndexPath中的NSLogs主线程上正确显示.但无论我做什么,它们都不会在tableView中显示.我尝试使用点表示法访问name属性,以及valueForKey,但都没有工作.
这是我的代码...... 它是从FRC初始化程序调用的:
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
__fetchedResultsController = [self newFetchedResultsControllerWithSearch:nil]; // creates a new FRC
[self filterAllContactsIntoDictionary: __fetchedResultsController];
return [[__fetchedResultsController retain] autorelease];
}
- (void) filterAllContactsIntoDictionary: (NSFetchedResultsController *) frc
{
NSArray *fetchedIDs = [[frc fetchedObjects] valueForKey:@"objectID"];
NSArray *fetched = [frc fetchedObjects];
if (filterMainQueue == nil) {
filterMainQueue = dispatch_queue_create("com.queue.FilterMainQueue", NULL);
}
dispatch_async(self.filterMainQueue, ^{
NSManagedObjectContext *backgroundContext = [[[NSManagedObjectContext alloc] init] autorelease];
[backgroundContext setPersistentStoreCoordinator:[[self.fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
NSMutableArray *backgroundObjects = [[NSMutableArray alloc] initWithCapacity: fetchedIDs.count];
// load the NSManagedObjects in this background context
for (NSManagedObjectID *personID in fetchedIDs)
{
Person *personInContext = (Person *) [backgroundContext objectWithID: personID];
[backgroundObjects addObject:personInContext];
}
[self internal_filterFetchedContacts: backgroundObjects]; // loads contacts into custom buckets
// done loading contacts into character buckets ... reload tableview on main thread before moving on
dispatch_async(dispatch_get_main_queue(), ^{
CGPoint savedOffset = [self.tableView contentOffset];
[self.tableView reloadData];
[self.tableView setContentOffset:savedOffset];
});
});
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?有没有其他方法可以明确地让Person对象在主线程上触发它们的错误?或者我是否对GCD队列和核心数据做错了,我不知道?谢谢.
小智 8
为什么不采取简单的路线,因为你没有保存任何新的东西?不是为后台线程创建额外的上下文并使用ID,managedObjectContext而是在锁定后使用后台线程中的main .
例如:
- (void) filterAllContactsIntoDictionary: (NSFetchedResultsController *) frc
{
if (filterMainQueue == nil) {
filterMainQueue = dispatch_queue_create("com.queue.FilterMainQueue", NULL);
}
dispatch_async(self.filterMainQueue, ^{
NSManagedObjectContext *context = ... // get the main context.
[context lock]; // lock the context.
// do something with the context as if it were on the main thread.
[context unlock]; // unlock the context.
dispatch_async(dispatch_get_main_queue(), ^{
CGPoint savedOffset = [self.tableView contentOffset];
[self.tableView reloadData];
[self.tableView setContentOffset:savedOffset];
});
});
}
Run Code Online (Sandbox Code Playgroud)
当我用方法调用方法时performSelectorInBackground,这对我有用,所以我猜它也适用于GCD调度.
| 归档时间: |
|
| 查看次数: |
3610 次 |
| 最近记录: |