我基本上需要在后台线程中更新我的核心数据而不阻塞 UI 并保存它。保存后应重新加载表视图以查看更改。所以为此我想到了使用
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
self.backgroundManagedObjectContext = context;
if(self.managedObjectContext == nil)
self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
self.backgroundManagedObjectContext.parentContext = self.managedObjectContext;
//update data
[self getDataFromFile];
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];
[context release];
});
});
当我尝试获取数据时在 getDataFromFile
if(![NSThread isMainThread])
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:self.backgroundManagedObjectContext];
[request setEntity:entity];
logs = [self.backgroundManagedObjectContext executeFetchRequest:request error:nil];
}
我收到错误*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“LogDetails”的 NSManagedObjectModel
谁能解释我为什么会收到此错误
另外,我还有另一个疑问是将其作为后台 managedObjectContext 还是将其作为父线程作为主线程 managedObjectContext 的子 managedObjectContext