开发者

How to properly multi-thread with Core Data? [closed]

开发者 https://www.devze.com 2022-12-12 18:39 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,o开发者_如何转开发r an extraordinarily narrow situation that is not
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, o开发者_如何转开发r an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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?


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


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号