我想知道崩溃堆栈中是否有任何内容可以帮助我了解“枚举时对集合<__ NSCFSet:0x600000986940>进行了变异”的根源。错误。根据到目前为止的了解,枚举时调用保存上下文可能会导致此问题。虽然,我认为使用.main.async可以解决此问题。它不是。我想我需要弄清楚如何实现并发,但是首先我想知道问题正在发生在哪里。
0 CoreFoundation 0x0000000106c4f6fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000105c72ac5 objc_exception_throw + 48
2 CoreFoundation 0x0000000106c4c5cc __NSFastEnumerationMutationHandler + 124
3 CoreData 0x00000001065c688f -[NSManagedObjectContext executeFetchRequest:error:] + 2463
4 ncdrainage 0x0000000104f18e64 $s10ncdrainage15ApplicationDataC18deleteLocalRecords04listF7DeletedySDyS2SG_tF + 1572
5 ncdrainage 0x0000000104f106db $s10ncdrainage15ApplicationDataC15downloadUpdates13finishClosureyySo23UIBackgroundFetchResultVc_tFySo19CKServerChangeTokenCSg_Sbs5Error_pSgtcfU1_ySo14CKRecordZoneIDC_AJ10Foundation0C0VSgSbALtcfU2_ + 1115
6 ncdrainage 0x0000000104f2f09b $s10ncdrainage15ApplicationDataC15downloadUpdates13finishClosureyySo23UIBackgroundFetchResultVc_tFySo19CKServerChangeTokenCSg_Sbs5Error_pSgtcfU1_ySo14CKRecordZoneIDC_AJ10Foundation0C0VSgSbALtcfU2_TA + 59
7 ncdrainage 0x0000000104f109a4 $sSo14CKRecordZoneIDCSo19CKServerChangeTokenCSg10Foundation4DataVSgSbs5Error_pSgIeggggyg_AbESo6NSDataCSgSbSo7NSErrorCSgIeyByyyyy_TR + 372
8 CloudKit 0x00000001054f26a7 -[CKFetchRecordZoneChangesOperation _handleProgressCallback:] + 4864
9 CloudKit 0x00000001054ba54f -[CKOperation _handleProgressCallback:completion:] + 61
10 CloudKit 0x00000001054b6d3c __21-[CKOperation _start]_block_invoke.370 + 216
11 CloudKit 0x00000001055306c5 __68-[CKOperationCallbackManager _performCallbackForOperation:callback:]_block_invoke + 278
12 libdispatch.dylib 0x000000010922d725 _dispatch_block_async_invoke2 + 83
13 libdispatch.dylib 0x000000010921fdb5 _dispatch_client_callout + 8
14 libdispatch.dylib 0x0000000109227225 _dispatch_lane_serial_drain + 778
15 libdispatch.dylib 0x0000000109227ed0 _dispatch_lane_invoke + 477
16 libdispatch.dylib 0x0000000109231ea3 _dispatch_workloop_worker_thread + 733
17 libsystem_pthread.dylib 0x0000000109608611 _pthread_wqthread + 421
18 libsystem_pthread.dylib 0x00000001096083fd start_wqthread + 13
答案 0 :(得分:0)
它没有崩溃堆栈中的内容那么多,而没有崩溃堆栈中的一切。由于没有类似NSManagedObjectContextPerform的东西,因此您似乎无法在正确的线程中使用上下文。您应该从提交给NSManagedObjectContext :: perform或NSManagedObjectContext :: performAndWait的块中调用处理上下文的代码。
所以代码应该是这样的:
func deleteObjects(context: NSManagedObjectContext) {
context.perform {
let objects = context.fetch(fetchRequest)
for object in objects {
context.delete(object)
}
context.save()
}
}
您可以通过启用core data concurrency debugging在调试中捕获此类问题。