objective-c - 核心数据 : missing variable binding for (null)

标签 objective-c core-data prefetch

在 Core Data 中,我有一个需要批量故障的计算案例。我使用以下方法来完成它,同时我预取了一些链接的实体。大多数时候一切正常,但偶尔会有一些用户崩溃:

Fatal Exception: NSInternalInconsistencyException
missing variable binding for (null)

这是我使用的方法:

NSFetchRequest *request = [CoreDataWrapper fetchRequestForEntity:entityName inManagedObjectContext:aContext];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@", entities];
[request setPredicate:predicate];
[request setReturnsObjectsAsFaults:NO];
[request setRelationshipKeyPathsForPrefetching:entityPaths];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

NSError *error;
NSArray *results = [aContext executeFetchRequest:request error:&error];

return results;

有什么想法吗?非常感谢!

最佳答案

为此联系 Apple 后,我得到的答复如下:

  1. 仅使用基于队列的上下文,这意味着在您需要创建 NSManagedObjectContext 实例时使用 NSPrivateQueueConcurrencyType 或 NSMainQueueConcurrencyType 调用 - (instancetype)initWithConcurrencyType:。

  2. 使用 performBlock(AndWait) 包装访问上下文及其对象的代码。

  3. 如果您需要跨不同上下文传递 Core Data 对象,请使用 NSManagedObjectID,而不是 NSManagedObject。

在我的例子中,我有一种在后台线程中使用核心数据的老方法,所以我需要在我的代码中使用 performBlock。

就是这样!

关于objective-c - 核心数据 : missing variable binding for (null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535491/

相关文章:

提前打字,猎犬 : remote works but not prefetch

google-chrome - Chrome 预取域中的页面

iphone - 自定义 UITableViewCell 中的 UITextView 不响应 didSelectRowAtIndexPath

ios - 我如何从 coreData swift 获取用户最近的聊天记录

iOS - 如何将 AppDelegate 的 ManagedObjectContext 传递给来自 NavigationView 的 UIViewController

ios - 最佳实践 : exposing NSManagedObject in framework

iOS CoreData后台线程在cellForRowAtIndexPath中获取

c++ - 如何预取不常用的代码?

ios - MKPolyline 未显示在 MKMapView 上

ios - Big Nerd Ranch,第 19 章,通过重新分配指针避免强引用循环