我有一个双重托管对象上下文设置,其中我有一个父/子 MOC 关系。父级负责直接写入私有队列(NSPrivateQueueConcurrencyType)上的数据库,然后我有一个子上下文负责为主线程(NSMainQueueConcurrencyType)拉取和保存数据。
当我对主队列上下文进行更改并保存它时,上下文将按应有的方式工作,然后将更改合并到后台队列上下文并在后台线程上写入数据库。
我遇到的问题是当我将数据直接写入后台队列上下文然后尝试将其合并到主队列上下文时。对象正确存储在商店中,当我合并更改时,它似乎可以工作。但是,如果我尝试在将数据保存到存储后直接针对主队列上下文发出 NSFetchRequest,那么数据是陈旧的,而不是正确的更新数据。
以下是代码摘录,应该可以帮助您了解我在做什么。需要注意的是从 JSON 返回 NSManagedObjects 的逻辑可以正常工作。此外,我已经测试并且在 UI 尝试对主队列 MOC 发出任何获取请求之前运行 mergeChangesFromContextDidSaveNotification。大家有什么想法吗?
// 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];
}