托管对象上下文不合并背景上下文中的更改

时间:2013-05-10 12:56:32

标签: ios core-data

我有一个双重托管对象上下文设置,我有一个父/子MOC关系。父负责直接写入私有队列(NSPrivateQueueConcurrencyType)上的数据库,然后我有一个子上下文负责拉取和保存主线程的数据(NSMainQueueConcurrencyType)。

上下文的工作方式应该像我在更改主队列上下文并保存时一样,然后将更改合并到后台队列上下文并在后台线程上写入数据库。

我遇到的问题是当我将数据直接写入后台队列上下文然后尝试将其合并到主队列上下文时。对象正确存储在商店中,当我合并更改时,它似乎工作。但是,如果我在将数据保存到存储后直接尝试对主队列上下文进行NSFetchRequest,则数据是陈旧的,而不是正确的更新数据。

以下代码摘录应该有助于让您全面了解我在做什么。需要注意的一些事项是从JSON正确返回NSManagedObjects的逻辑。此外,我已经测试并且mergeChangesFromContextDidSaveNotification在UI尝试对主队列MOC进行任何获取请求之前运行。你们都有什么想法吗?

// Context Setup
- (NSManagedObjectContext *)backgroundWriterManagedObjectContext
{
    if (_backgroundWriterManagedObjectContext != nil)
        return _backgroundWriterManagedObjectContext;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        _backgroundWriterManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        _backgroundWriterManagedObjectContext.persistentStoreCoordinator = coordinator;
        _backgroundWriterManagedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
        _backgroundWriterManagedObjectContext.undoManager = nil;
    }
    return _backgroundWriterManagedObjectContext;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil)
        return _managedObjectContext;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        _managedObjectContext.parentContext = _backgroundWriterManagedObjectContext;
        _managedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
        _managedObjectContext.stalenessInterval = 0.0;
    }
    return _managedObjectContext;
}

// save MOC
- (void)saveManagedObjectContext
{
    @try
    {
        // perform synchronous process to save to the main MOC
        [_managedObjectContext performBlockAndWait:^(void)
        {
            __block NSError *error = nil;

            // push any changes in the main context to the background writer context
            if (![_managedObjectContext tryLock])
                [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Unable To Lock Managed Object Context: %@", error.localizedDescription]];

            if (![_managedObjectContext save:&error])
                [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Error Saving Managed Object Context: %@", error.localizedDescription]];

            [_managedObjectContext unlock];

            // save the background writer context
            [_backgroundWriterManagedObjectContext performBlock:^(void)
            {
                error = nil;

                if (![_backgroundWriterManagedObjectContext tryLock])
                    [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Unable To Lock Background Writer Managed Object Context: %@", error.localizedDescription]];

                if (![_backgroundWriterManagedObjectContext save:&error])
                    [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Error Saving Background Writer Managed Object Context: %@", error.localizedDescription]];

                [_backgroundWriterManagedObjectContext unlock];
            }];
        }];
    }
    @catch (NSException *exception)
    {
        [VS_Log VS_LogException:exception];
    }
}

// This code runs when attempting to save objects from a web service call
[_backgroundWriterManagedObjectContext performBlock:^(void)
    {
        // create a new process object and add it to the dictionary
        __block VS_CoreDataRequest *request = [[VS_CoreDataRequest alloc] init];

        // .. LOGIC HERE DESERIALIZES THE JSON AND RETURNS AN ARRAY OF NSMANAGEDOBJECTS

         // perform synchronous process to save to the main MOC
         @try
         {
             NSError *error = nil;

             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChangesFromContextDidSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:_backgroundWriterManagedObjectContext];

             if (![_backgroundWriterManagedObjectContext tryLock])
                 [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Unable To Lock Background Writer Managed Object Context: %@", error.localizedDescription]];

             if (![_backgroundWriterManagedObjectContext save:&error])
                 [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManger - Error Saving Background Writer Managed Object Context: %@", error.localizedDescription]];

             [_backgroundWriterManagedObjectContext unlock];

             // submit changes back to the forground context
             [_managedObjectContext performBlock:^(void)
             {
                 NSMutableArray *objects = [[NSMutableArray alloc] init];

                 // iterate through the updated objects and find them in the main thread MOC
                 for (VS_BaseManagedObject *object in request.objects)
                 {
                     // get the object from the main managed object context
                     NSError *error;
                     NSManagedObject *obj = [_managedObjectContext existingObjectWithID:object.objectID error:&error];

                     if (error)
                         [VS_Log VS_LogError:[NSString stringWithFormat:@"VS_CoreDataManager - Error: %@", error.localizedDescription]];

                     if (obj)
                     {
                         [_managedObjectContext refreshObject:obj mergeChanges:YES];
                         [objects addObject:obj];
                     }
                 }

                 // RETURN FROM METHOD SO UI CAN REGAIN CONTROL
             }];
         }
         @catch (NSException *exception)
         {
            [VS_Log VS_LogException:exception];
            return;
         }
     }];

// Merges changes from parent to child context
- (void)mergeChangesFromContextDidSaveNotification:(NSNotification *)notification
{
    // remove observer
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:_backgroundWriterManagedObjectContext];

    // merge the changes
    [_managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}

1 个答案:

答案 0 :(得分:0)

您没有调用进程待处理的更改。