由于未捕获的异常'NSGenericException',同步解决了终止应用程序,原因:'***集合<__ NSCFSet:>在枚举时发生了变异

时间:2019-08-01 00:22:40

标签: ios swift xcode grand-central-dispatch

我相信我已经找到了解决该错误的方法,但是不知道我是通过减慢代码速度来避免我的解决方案出现竞争状况,还是真正解决了该问题。

我的问题是2部分
1)为什么将dispatch.main.async更改为dispatch.main.sync可以使错误消失,因为我已经正确地对其进行了编码或者我现在只是解决该问题?

2)我是否需要将另外两个通话的所有dispatch.main.async更改为dispatch.main.sync

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x600002a24180> was mutated while being enumerated.

在此报告中,第二个答案专门说,永远不要使用dispatch.main.sync,因为它会导致死锁。第一个答案是,您可能要使用同步,但它可能导致死锁。

What does main.sync in global().async mean?

Apple报告以下错误:

Thread 5 name:  Dispatch queue: com.apple.cloudkit.operation-7BCFBC195B4B17C3.callback
Thread 5 Crashed:
0   libsystem_kernel.dylib          0x00000001b4e120dc 0x1b4def000 + 143580
1   libsystem_pthread.dylib         0x00000001b4e8b094 0x1b4e89000 + 8340
2   libsystem_c.dylib               0x00000001b4d6af4c 0x1b4d10000 + 372556
3   libsystem_c.dylib               0x00000001b4d6aeb4 0x1b4d10000 + 372404
4   libc++abi.dylib                 0x00000001b4437788 0x1b4436000 + 6024
5   libc++abi.dylib                 0x00000001b4437934 0x1b4436000 + 6452
6   libobjc.A.dylib                 0x00000001b444ee00 0x1b4449000 + 24064
7   libc++abi.dylib                 0x00000001b4443838 0x1b4436000 + 55352
8   libc++abi.dylib                 0x00000001b44438c4 0x1b4436000 + 55492
9   libdispatch.dylib               0x00000001b4cb47e8 0x1b4c54000 + 395240
10  libdispatch.dylib               0x00000001b4c5d324 0x1b4c54000 + 37668
11  libdispatch.dylib               0x00000001b4c5de74 0x1b4c54000 + 40564
12  libdispatch.dylib               0x00000001b4c664ac 0x1b4c54000 + 74924
13  libsystem_pthread.dylib         0x00000001b4e95114 0x1b4e89000 + 49428
14  libsystem_pthread.dylib         0x00000001b4e97cd4 0x1b4e89000 + 60628

当我在Xcode中运行代码时,我可以在Xcode中重现该错误并获取以下堆栈跟踪。

2019-07-31 19:51:07.893799-0400 My Toy Chest[49278:1080065] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x600002a24180> was mutated while being enumerated.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001106d38db __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010f6f4ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001106d07ac __NSFastEnumerationMutationHandler + 124
    3   CoreData                            0x0000000110048dd4 -[NSManagedObjectContext executeFetchRequest:error:] + 3332
    4   My Toy Chest                        0x0000000109248bc0 $s12My_Toy_Chest21ActionFigureSpecificsC03setdeF8CoreDataSbyF + 1472
    5   My Toy Chest                        0x000000010923a4b2 $s12My_Toy_Chest21ActionFigureSpecificsC03setdeF0yyF + 546
    6   My Toy Chest                        0x0000000109313c6b $s12My_Toy_Chest22SharedDataActionFigureC03getfg26SpecificsICloudForCompletefG5ArrayyyFySaySo8CKRecordCGSg_s5Error_pSgtcfU_ + 3051
    7   My Toy Chest                        0x000000010931409c $s12My_Toy_Chest22SharedDataActionFigureC03getfg26SpecificsICloudForCompletefG5ArrayyyFySaySo8CKRecordCGSg_s5Error_pSgtcfU_TA + 12
    8   My Toy Chest                        0x0000000109315233 $s12My_Toy_Chest22SharedDataActionFigureC013fetchCompletefG18SpecificsFromCloud10completionyySaySo8CKRecordCGSg_s5Error_pSgtc_tFySo13CKQueryCursorCSg_AKtcfU0_ + 1331
    9   My Toy Chest                        0x0000000109315e01 $s12My_Toy_Chest22SharedDataActionFigureC013fetchCompletefG18SpecificsFromCloud10completionyySaySo8CKRecordCGSg_s5Error_pSgtc_tFySo13CKQueryCursorCSg_AKtcfU0_TA + 49
    10  My Toy Chest                        0x00000001092373b4 $sSo13CKQueryCursorCSgs5Error_pSgIeggg_ACSo7NSErrorCSgIeyByy_TR + 132
    11  CloudKit                            0x000000010f51e84d -[CKQueryOperation _finishOnCallbackQueueWithError:] + 613
    12  CloudKit                            0x000000010f51795a -[CKOperation _finishInternalOnCallbackQueueWithError:] + 582
    13  CloudKit                            0x000000010f5176fa -[CKOperation _handleCompletionCallback:] + 148
    14  CloudKit                            0x000000010f51e521 -[CKQueryOperation _handleCompletionCallback:] + 197
    15  CloudKit                            0x000000010f58d438 __82-[CKOperationCallbackManager _performCallbackForOperation:callback:errorCallback:]_block_invoke + 288
    16  libdispatch.dylib                   0x000000011189e725 _dispatch_block_async_invoke2 + 83
    17  libdispatch.dylib                   0x0000000111890db5 _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x0000000111898225 _dispatch_lane_serial_drain + 778
    19  libdispatch.dylib                   0x0000000111898ed0 _dispatch_lane_invoke + 477
    20  libdispatch.dylib                   0x00000001118a2ea3 _dispatch_workloop_worker_thread + 733
    21  libsystem_pthread.dylib             0x0000000111c79611 _pthread_wqthread + 421
    22  libsystem_pthread.dylib             0x0000000111c793fd start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我很确定发生缺陷的代码是下面的代码集,它们更新了View Controller集合视图中的UI。

以下代码崩溃(异步)

    DispatchQueue.main.async {
        self.delegate?.updateActionFigureSpecificsModel()
    }

以下代码不会崩溃(同步)

    DispatchQueue.main.sync {
        self.delegate?.updateActionFigureSpecificsModel()
    }

随后的调用会更新View Controller集合视图

    func updateActionFigureSpecificsModel() {
        DispatchQueue.main.async {
            self.delegate?.updateActionFigureModel(forActionFigure: self, forDelegateProperty: .specifics)
        }
    }
    func updateActionFigureModel(forActionFigure actionFigure: ActionFigure, forDelegateProperty delegateProperty: DelegateProperty) {
        switch delegateProperty {
        case .specifics, .eBaySaleSummary:
            DispatchQueue.main.async {
                self.setUIActionFigure(forActionFigure: actionFigure)
            }
        }
    }

    func setUIActionFigure(forActionFigure actionFigure: ActionFigure) {
        if let indexPath = getIndexPath(forActionFigure: actionFigure) {
            DispatchQueue.main.async {
                self.collectionView.reloadItems(at: [indexPath])
            }
        }

1 个答案:

答案 0 :(得分:1)

我真的不能从您发布的内容中分辨出确切的问题是什么,但是您可能遇到了以下一个或两个普遍问题:

  • 基础集合不是线程安全的,不能从多个线程进行修改。
  • 基础集合在迭代时无法修改。

我怀疑您在运行的任何线程中使用/迭代/修改的集合正在与您在main上分派的任务所完成的使用/迭代/修改相冲突。是的,我怀疑将async更改为sync只是通过在后台线程恢复之前等待main任务完成来避免问题。 通常,如果后台线程和主线程共享对同一个基础集合的引用-并且它们中的一个都修改了该集合-最终会遇到问题...非常小心。

我经常使用的解决方案是将收藏夹分开。我的后台线程将进行更新或执行计算以更新集合。然后,我将制作一个集合的副本,并将该副本传递到main进行显示和UI交互。