【问题标题】:Core Data determining where save and fetch operations conflict核心数据确定保存和获取操作冲突的位置
【发布时间】:2017-08-16 17:44:53
【问题描述】:

tldr:在 coredata fetch 期间,我的应用程序崩溃并出现错误:“collection mutated while enumerated”,我想找出导致数据发生变化并因此导致崩溃的 coredata 保存。

我正在使用 Magical Records 对 Core Data 执行 100 次提取操作。这些获取操作在后台线程中执行。当这些提取运行时,我在我的应用程序的某个地方执行了保存。现在,显然当此保存发生时,我的应用程序崩溃,因为它更改了其他 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 时遇到了很多麻烦,而且似乎没有任何好的工具或资源可供我使用,是否有人有任何资源可供我查看?

【问题讨论】:

  • 是的,我已经读过了。我添加了标志:-com.apple.CoreData.ConcurrencyDebug 1,现在正在处理它引发无效操作异常的地方,但我认为这不是问题所在。

标签: ios objective-c multithreading core-data magicalrecord


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2011-01-11
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多