当我尝试执行提取时,我的应用似乎崩溃了。我使用了神奇的记录。错误消息是:
Collection< __ NSCFSet:0x17005a1f0>在被列举时被突变。
对我而言,这表明我们在执行提取时正在更改上下文中的对象,但我是新手,所以我可能错了。
以下是它指向的代码:
- (void) buildAndFetchFRCsInContext:(NSManagedObjectContext*)context{
[context performBlock:^{
__unused NSDate* start = [NSDate date];
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];
NSError* error;
// Peform the fetches
[self.contactsFRC performFetch:&error];
[self.callsFRC performFetch:&error];
[self.newsItemsFRC performFetch:&error]; //Crash points to this line
NSLog(@"Spent [%@s] performing fetchs for counts!", @(fabs([start timeIntervalSinceNow])));
[self calculateAndBroadcastCounts];
}];
}
传入的上下文是:
- (instancetype) initWithUserSession:(BPDUserSession*)userSession{
self = [super init];
...
self.context = [NSManagedObjectContext MR_context];
[self buildAndFetchFRCsInContext:self.context];
...
}
我认为这个类是在主线程中初始化的,但是performBlock将块添加到队列中,然后从不同的线程执行。但我不认为这是真的,因为performBlock的目的是在另一个线程上执行该块。
根据我发布的内容,任何人都可以说出问题所在吗?
我尝试将buildFetchResultsController
调用移到执行块之外:
- (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]; // Generally error is thrown on i = 5 ~> 10
}
NSLog(@"Spent [%@s] performing fetchs for counts!", @(fabs([start timeIntervalSinceNow])));
[self calculateAndBroadcastCounts];
}];
}
但这仍然失败。我能够通过上面显示的循环重现故障。我还尝试使用NSPrivateQueueConcurrencyType
在实际的performBlock闭包中创建一个新的上下文,但这也没有用,同样的问题。
注意:我使用的是MagicalRecords,所以对于那些不熟悉的人来说,[NSManagedObjectContext MR_context];
等同于从以下位置返回的上下文:
NSManagedObjectContext *context = [[self alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; // Here self is NSManagedObjectContext
[context setParentContext:parentContext];
[context MR_obtainPermanentIDsBeforeSaving];
return context;
答案 0 :(得分:2)
我的问题是我在上下文中执行保存并且保存了并发问题,我试图从非主要线程保存在主线程上创建的对象。将该代码移动到主线程修复了崩溃。