iOS:来自 iOS14 EXC_BAD_ACCESS 线程队列的随机错误消息:NSManagedObjectContext NSInternalAdditions forgetObject propagateToObjectStore

iOS: Random error message from iOS14 EXC_BAD_ACCESS thread Queue: NSManagedObjectContext NSInternalAdditions forgetObject propagateToObjectStore

自从新安装 iOS 14.0 以来,我有一个奇怪的错误,当我点击一个表格视图时,或者当我点击一个按钮时,或者当我打开一个弹出窗口时。这是非常随机的,很难完美地重现。 xcode的console 不是很会讲这个问题。我在线程中有此错误消息:

//错误示例 1

#4 0x0000000189c378e8 in -[NSManagedObjectContext(_NSInternalAdditions) _forgetObject:propagateToObjectStore:removeFromRegistry:] ()

在主视图中:

Thread 12: EXC_BAD_ACCESS (code=1, address=0x4)

//错误示例 2

//错误示例 3

它说 NSManagedObjectContext 的线程导致了崩溃。
我在获取有关该错误的更多信息时遇到问题...

有什么想法吗?

该错误表明在不同的线程上执行了 CoreData 操作。

根据 doc,您可以使用 perform()(异步)或 performAndWait()(同步)来确保您在正确的队列中进行调用。

You use contexts using the queue-based concurrency types in conjunction with perform(:) and performAndWait(:). You group “standard” messages to send to the context within a block to pass to one of these methods. There are two exceptions:

Setter methods on queue-based managed object contexts are thread-safe. You can invoke these methods directly on any thread.

If your code is executing on the main thread, you can invoke methods on the main queue style contexts directly instead of using the block based API.

perform(:) and performAndWait(:) ensure the block operations are executed on the queue specified for the context. The perform(:) method returns immediately and the context executes the block methods on its own thread. With the performAndWait(:) method, the context still executes the block methods on its own thread, but the method doesn’t return until the block is executed.

当您初始化上下文时,它将附加到某个队列,您不知道是哪个队列,但是通过调用该方法(执行或执行并等待)您将确保以正确的方式进行一.