核心数据确定保存和获取操作冲突的位置

Core Data determining where save and fetch operations conflict

tldr:在获取核心数据期间,我的应用程序因错误而崩溃:'collection mutated while enumerated' 我想找出正在改变数据并因此导致此崩溃的核心数据保存。

我正在使用 Magical Records 对 Core Data 执行 100 次提取操作。这些获取操作在后台线程中执行。虽然这些提取是 运行ning,但我在我的应用程序的某个地方执行了保存。现在,显然当这个保存发生时,我的应用程序崩溃了,因为它改变了另一个 ManagedObjectContext 中的数据。

我找不到保存的位置,因为它是 运行 来自不同的线程,所以最远的回溯是“开发人员提交块到 developerSubmittedBlockToNSManagedObjectContextPerform

首先,当我使用单独的上下文执行提取时,另一个线程上的另一个上下文的更改如何导致我崩溃?

我怎么知道这个存档在哪里?

我该如何解决这个问题?

如何针对这种情况不那么明显的其他实例调试应用程序?

这就是我执行 100 个提取请求的方式:

- (instancetype) initWithUserSession:(BPDUserSession*)userSession{
    ...
    self.context = [NSManagedObjectContext MR_context]; // new context, root is parent, private type
    [self buildAndFetchFRCsInContext:self.context];
    ...
}

- (void) buildAndFetchFRCsInContext:(NSManagedObjectContext*)context {
self.contactsFRC = [self buildFetchResultsControllerForClass:[Contact class] sortedBy:@"id" withPredicate:nil inContext:context];
self.callsFRC = [self buildFetchResultsControllerForClass:[Call class] sortedBy:@"id" withPredicate:nil inContext:context];
self.newsItemsFRC = [self buildFetchResultsControllerForClass:[NewsItem class] sortedBy:@"id" withPredicate:nil inContext:context];

NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity:100];
for (int i = 0; i < 100; i++) {
    list[i] = [self buildFetchResultsControllerForClass:[NewsItem class] sortedBy:@"id" withPredicate:nil inContext:context];
}

[context performBlock:^{
    __unused NSDate* start = [NSDate date];

    NSError* error;

    // Peform the fetches
    [self.contactsFRC performFetch:&error];
    [self.callsFRC performFetch:&error];
    [self.newsItemsFRC performFetch:&error];

    for (int i = 0; i < list.count; i++) {
        [list[i] performFetch:&error];
    }

    NSLog(@"Spent [%@s] performing fetchs for counts!", @(fabs([start timeIntervalSinceNow])));

    [self calculateAndBroadcastCounts];
}];
}

总的来说,我在调试 Core Data 时遇到了很多麻烦,而且似乎没有任何我可以利用的好工具或资源,有没有人有任何资源供我浏览?

问题是我从不是创建对象的上下文中保存对象。所以当我 运行 在这里获取时,它有时会与保存调用发生冲突,我们会崩溃。