NSFetchedResultsController在从后台线程合并更新后不触发委托方法 [英] NSFetchedResultsController not firing delegate method after merging update from background thread
问题描述
我有一个NSFetchedResultsController和几个操作,通过NSOperationQueue在不同的线程插入和更新托管对象。
I have an NSFetchedResultsController and a few operations that inserts and updates managed objects on separate threads via NSOperationQueue.
FRC看起来像这样, cache to nil:
The FRC looks like this, note that I have set the cache to nil:
[NSFetchedResultsController deleteCacheWithName:nil];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
每个线程操作都有自己的管理对象上下文,并在每次保存更改时向主MOC调用mergeChangesFromContextDidSaveNotification 。
Each threaded operation has its own managed object context and fires mergeChangesFromContextDidSaveNotification to the main MOC each time it saves changes.
我用来合并上下文的代码如下:
The code that I use to merge contexts looks like this:
- (void)mergeContextChanges:(NSNotification *)notification
{
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
if([NSThread isMainThread] == NO)
{
[self performSelectorOnMainThread:_cmd withObject:notification waitUntilDone:NO];
return;
}
NSSet *updated = [[notification userInfo] objectForKey:NSUpdatedObjectsKey];
for(NSManagedObject *thing in updated)
{
NSLog(@"Background thread updated %@", [thing description]);
}
for(NSManagedObject *thing in updated)
{
[[context objectWithID:[thing objectID]] willAccessValueForKey:nil];
}
[context mergeChangesFromContextDidSaveNotification:notification];
}
我可以通过查看日志来确认每次后台操作插入或更新数据,我的mergeContextChanges:方法正在使用正确的插入/更新值调用。
I can confirm by looking at the logs that each time the background operations insert or update data, my mergeContextChanges: method is being called with the proper insert/update values.
问题是,在合并插入 FRCs委派方法(例如controllerDidChangeContent :)正确,合并更新不会通知FRC触发其委托方法。
The problem is that while merging inserts are firing the FRCs delegate methods (ex. controllerDidChangeContent:) properly, merging updates doesn't signal the FRC to fire its delegate methods.
很奇怪,如果我使用主MOC 在主线程上运行更新,我还可以确认FRC正确地激活其代理。
Strange enough, I can also confirm that the FRC fires its delegates properly if I run the updates on the main thread using the main MOC.
如何在合并更新的MOC时使FRC触发其委托方法?
How can I make the FRC fire its delegate methods when updated MOCs are merged?
更多信息:看起来像使用任何MOC除了主MOC并尝试将更新合并到主MOC具有相同的结果; FRC拒绝注意。
More Info: Looks like using any MOC other than the main MOC and trying to merge updates to the main MOC has the same results; the FRC refuses to notice it.
推荐答案
哦,男人。
看起来像在我的线程操作中访问主MOC(即使它是与我尝试更新的数据无关的任务)导致这种奇怪的行为。
It looks like accessing the main MOC from within my threaded operations (even if it's for a task unrelated to the data I was trying to update) causes this weird behavior.
我希望这可以帮助遇到这个问题的任何人。
I hope this helps anyone else that runs into this problem.
这篇关于NSFetchedResultsController在从后台线程合并更新后不触发委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!