如何正确使用 Core Data 进行多线程?

发布于 2024-08-12 02:13:59 字数 796 浏览 1 评论 0原文

我在尝试在线程 NSOperation 中执行一些核心数据操作时遇到很多问题。

目前,我已经在我的应用程序委托中创建了一个托管对象上下文,该上下文纯粹用于我的线程 NSOperations。我设置了一个最大并发数为 1 的 NSOperationQueue,因此每个操作都是串行执行的。对于每个操作(从互联网获取数据并创建新的托管对象),我都会将其传递给要使用的上下文。创建新对象后,我保存并重置上下文以供下一个操作使用。然而,我在执行此操作时间歇性地遇到 malloc 错误,我花了几天时间试图弄清楚它,它似乎与它的线程方式有关。

我遇到的错误可以在这个相关的堆栈溢出问题中看到。

当我只是将托管对象的属性设置为 NSNumber 对象时,就会发生 malloc 错误。这也是我第一次设置该属性,所以我并没有过度释放自己!我根本无法弄清楚,并且无法使用 GuardMalloc 重现该错误,它只是不会发生!就好像错误发生在其他地方被触发,但我不知道在哪里!

我只能推断出这个错误与我如何设置整个事情有关。我尝试自己运行该操作,而不是将其添加到 NSOperationQueue 中,它似乎工作正常(尽管它挂起了主线程!)。

当保存完成时,我还需要上下文来通知应用程序中的其他上下文,因此我观察到它是 NSManagedObjectContextDidSaveNotification 通知。但是,由于保存是在 NSOperation(其他线程)中执行的,因此是否会出现问题,因为通知仅在其运行的线程上分派?

I'm having a lot of issues with trying to perform some core data operations in a threaded NSOperation.

Currently, I have created a managed object context in my app delegate that is purely used on my threaded NSOperations. I setup a NSOperationQueue with a max concurrency of 1 so each action is performed serially. For each operation (that gets data from the internet and creates new managed objects), I pass it the context to use. Once the new objects have been created, I save and reset the context for the next operation to use. However, I'm intermittently getting malloc errors while doing this, and I've spent days trying to figure it out, and it would appear to have something to do with how it is threaded.

The errors I'm getting can be seen in this related stack overflow question.

The malloc error is occurring when I'm simply setting a managed object's property to an NSNumber object. It is also the first time I'm setting that property so it is nothing that I am over releasing myself! I can't figure it out at all, and I can't reproduce the error using GuardMalloc, it just doesn't occur! It's as if the error is happening somewhere else that is being triggered, but I have no idea where!

All I can deduce then is that the error is somehow connected with how I've set up the whole thing. I have tried running the operation myself instead of adding it to the NSOperationQueue and it appears to work fine (although it hangs the main thread!).

I also need the context to inform other contexts in my app when saves have been made, so I observe it's NSManagedObjectContextDidSaveNotification notification. However, since saves are being performed in the NSOperation (other thread), will there be problems with this as notifications are only dispatched on the thread it's running in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作妖 2024-08-19 02:13:59

您是否阅读过 核心数据编程指南的多线程部分?

Have you read the Multi-Threading with Core Data section of the Core Data Programming Guide?

弃爱 2024-08-19 02:13:59

复活一个老问题,但这可能会对某人有所帮助 - 我使用此处描述的相同设置遇到了类似的问题(每个 NSOperation 的专用上下文,最大并发数),并且我发现这是由于我正在创建的事实主线程上的 NSOperation 专用上下文,然后我尝试在 NSOperation 线程中使用它。一旦我将上下文创建移至 NSOperation 的主函数中,问题就消失了。

Resurrecting an old question, but this might help someone - I ran into similar problems with the same setup described here (dedicated context for each NSOperation, max concurrency of one), and I found out that it was due to the fact that I was creating the NSOperation-dedicated context on the main thread, and then I tried to use it in an NSOperation thread. Once I moved context creation into the NSOperation's main function, the problems were gone.

轻拂→两袖风尘 2024-08-19 02:13:59

是的,我现在已经成功地让一切正常工作了,这让我松了一口气。经过几天的摸索,我决定重写所有与线程和核心数据有关的代码,并且我不再收到 malloc 错误。这是一个相当复杂的设置,所以里面一定有一些非常模糊的东西!

Right, I've managed to get it all working now, much to my relief. After days of messing around, I decided to rewrite all the code to do with the threading and core data and I'm no longer getting the malloc errors. It is a rather complex setup so there must have been something pretty obscure in there!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文