views:

510

answers:

3

Hi there,

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?

This is rather urgent and if anyone can help me get this set up properly or figure out why I'm getting this intermittent malloc error then I'd be very happy to give away a £20 Amazon or iTunes voucher!

Many thanks,

Michael

+1  A: 

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

Ben S
Yes I've read that, and I'm doing everything according to how it says you should. I'm using a separate context for the thread (it's even failing with just 1 `NSOperation` on the queue) and I'm not passing any managed objects between threads. Well, I am calling a method in my app delegate to return an object but the inside of the method is wrapped in a @synchronized(self) {} block and the managed object that it returns is in the thread's context.
Michael Waterfall
A: 

Right, I've managed to get it all working now much to my relief. After days of messing around I decided to re-write 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!

Michael Waterfall
A: 

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.

Vladimir Mitrovic