略从不同的NSManagedObjectContext合并的更改

略从不同的NSManagedObjectContext合并的更改

本文介绍了NSFetchedResultsController与谓词忽略从不同的NSManagedObjectContext合并的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用具有谓词的 NSFetchedResultsController 来呈现表视图内容: [NSPredicate predicateWithFormat:@visible ==%@,[NSNumber numberWithBool:YES]] 在后台线程使用单独的 NSManagedObjectContext 我更新少数实体并更改他们可见值从 NO 到 YES 。保存,合并主线程的 NSManagedObjectContext 中的更改。但 NSFetchedResultsController 的 fetchedObjects 不会更改。另外,控制器不会在代理上调用 -controller:didChangeObject:... 。 还有Notification的 NSUpdatedObjectsKey / code>包含这些对象。 目前我发现的唯一解决方案是调用 NSUpdatedObjectsKey entities: NSManagedObjectContext * context = ... //主线程上下文 [context existingObjectWithID: object objectID] error:nil] 此问题仅适用于更新的解决方案 div> 显示更新的对象的主要 NSManagedObjectContext didn't event fire NSManagedObjectContextObjectsDidChangeNotification 通用修复(或保留需要此处理的对象ID的轨道): NSManagedObjectContext * context = [self managedObjectContext]; for(NSManagedObject * object in [[notification userInfo] objectForKey:NSUpdatedObjectsKey]){ [[context objectWithID:[object objectID]] willAccessValueForKey:nil]; } [context mergeChangesFromContextDidSaveNotification:notification]; 从NSManagedObject类参考: 您可以使用键值nil调用此方法,以确保故障已触发,如下例所示 。 I am presenting table view contents using NSFetchedResultsController which has a predicate:[NSPredicate predicateWithFormat:@"visible == %@", [NSNumber numberWithBool:YES]]On background thread using separate NSManagedObjectContext I update few of the entities and change theirs visible value from NO to YES. Save, merge changes in main thread's NSManagedObjectContext. But NSFetchedResultsController's fetchedObjects doesn't change. Also controller doesn't call -controller:didChangeObject:... on delegate. If entities are updated on main thread in identical manner (my test app calls the same method), everything works as expected.Also Notification's NSUpdatedObjectsKey contains those objects.Currently the only solutions I've found is to call for each of NSUpdatedObjectsKey entities:NSManagedObjectContext *context = ... // main thread context[context existingObjectWithID:[object objectID] error:nil]This issue is only with updated objects which previously didn't match the predicate.Am I missing something obvious? 解决方案 Turns out main NSManagedObjectContext didn' t event fire NSManagedObjectContextObjectsDidChangeNotification for updated objects because it is not done for faulted objects.Generic fix (or keep a track of object IDs that needs this treatment):NSManagedObjectContext *context = [self managedObjectContext];for(NSManagedObject *object in [[notification userInfo] objectForKey:NSUpdatedObjectsKey]) { [[context objectWithID:[object objectID]] willAccessValueForKey:nil];}[context mergeChangesFromContextDidSaveNotification:notification];From NSManagedObject Class Reference: You can invoke this method with the key value of nil to ensure that a fault has been fired, as illustrated by the following example. 这篇关于NSFetchedResultsController与谓词忽略从不同的NSManagedObjectContext合并的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 13:36