后台核心数据更新 [英] core data update in background
问题描述
我需要在不阻塞UI的情况下在后台线程中基本更新我的核心数据并保存它.保存后应重新加载表View以查看更改.为此,我想到了使用
I need to basically update my core data in a background thread without blocking UI and save it. After saving should reload table View to view the changes. So for doing this I thought of using
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中
and in getDataFromFile when I try to fetch data
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
I get error * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'LogDetails''
谁能解释为什么我收到此错误
can anyone explain why I am getting this error
我还有另一个疑问,是将其作为后台托管对象上下文还是将子托管对象上下文与父级作为主线程托管对象上下文
Also I have an another doubt whether to include it as background managedObjectContext or child managedObjectContext with parent as main thread managedObjectContext
推荐答案
一个NSManagedContext仅应在一个线程中使用.在线程之间传递NSManagedObject可能是不安全的.
One NSManagedContext should only be used in one thread. passing NSManagedObject between threads are potentially unsafe.
这篇关于后台核心数据更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!