0

后台更新核心数据的更新版本。

在与 CoreData链接Grand Central Dispatch (GCD)的帮助下,创建了一个后台 managedObjectContext 但从核心数据获取时出现错误

-(void) startTimerThread
{
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add code here to do background processing
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
        self.backgroundManagedObjectContext = context;
        [self.backgroundManagedObjectContext setPersistentStoreCoordinator:self.managedObjectContext.persistentStoreCoordinator];
        self.managedObjectContext = self.backgroundManagedObjectContext;
        [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];
        self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    });
});

}

getDataFromFile我尝试从中获取时出现错误managedObjectContext

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setPredicate:[NSPredicate predicateWithFormat:@"date == max(date)"]];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:self.managedObjectContext];

同样的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name LogDetails

谁能告诉我为什么我得到这个错误。以前我试图创建一个子 managedObjectContext ,它给出了同样的错误。

提前致谢。

4

1 回答 1

1

我不太确定为什么要创建上下文并将其设置为 self.backgroundManagedObjectContext,然后将其设置为 self.managedObjectContext。我的建议是允许您的 getDataFromFile 方法接受上下文,以便您可以从任何线程调用它。

你将会拥有

- (void)getDataFromFileOnContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPredicate:[NSPredicate predicateWithFormat:@"date == max(date)"]];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogDetails" inManagedObjectContext:context];
}

然后你可以这样做

-(void) startTimerThread
{
    self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add code here to do background processing
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [context setParentContext:self.managedObjectContext];
        [self getDataFromFileOnContext:context];

    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];        
    });
});

如果您需要更多帮助,请告诉我。

于 2013-03-07T18:16:57.270 回答