Search code examples
objective-cmultithreadingcore-dataexc-bad-access

Core Data problems when using threads


I'm working on a hobby project which I'm slowly updating in my spare time to help learn some new things. One stumbling block I've come across is working with Core Data in a separate thread. I've read Apple's documentation about Core Data concurrency and everything I read seemed straight forward enough so I began to update my project to load data on a background thread as I don't want to lockout the UI whilst things are loading.

The project works fine if the Core Data object is loaded on the main thread. It crashes if I switch to background loading.

At this stage, I can verify that:

  • The NSManagedObject loads on the thread and I can access it's properties
  • Outputting the data to the stdout works fine and looks correct.
  • A binary comparson of the data object loaded on the main thread and the data loaded on the background thread proves they are identical.

The actual problem occurs when I call a category implementation on NSData. I can verify the NSData object is fine when it's loaded on the background thread, it's only when I call a function to do some work on the NSData object after it's loaded do I get a problem. The problem is a EXC_BAD_ACCESS, which usually means the address of an object is wrong but it doesn't quite make any sense.

I'm probably just getting something obvious or simple wrong - but I just can't see the forest for the trees.

If you think you can offer any advice on this as it's driving my crazy then you can find the code here:

Edit post answer: Removed URL as project no longer exists.


Solution

  • Ok I've finally found out what the problem is. The decompression method was exceeding the stack size of the thread and therefore causing a weird & random EXC_BAD_ACCESS to be fired.

    I would have expected the debugger to produce a more direct clue in this case.

    So a valid 'stack overflow' problem, solved.