0

I am using CATiledLayer to render NSManagedObjects. But you know, CATiledLayer render objects in background threads. This make my app crash on iOS5

I know that I should use separated NSManagedContext for each of threads but this way make performance get bad. (because I have to save the NSManagedContext more often to transfer data to other threads).

Do you guys know the better way to work around my problem? Please help!!!

Sorry for my poor English!

Xung Le
  • 21
  • 1
  • 3

1 Answers1

1

NSManagedObjectContext is not thread safe, nor are NSMangedObjects. You should create a MOC on the background thread, pass in any IDs (which ARE thread safe), and load them on the background thread context.

UPDATE:

One alternative is to create plain old obj-c objects, or even just a regular NSDictionay, which contains the necessary data and pass those to the background thread. So after your MO is populated, create a POOCO, copy in the necessary data, and pass that to your background thread for processing. This will avoid disk access.

logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • Thanks for answering me. But you know, You have to save the nsmanagedcontext on main thread before you can get the object via objectID in new thread. That make the performance become terrible in my app. I wonder is there a better solution for this problem? – Xung Le Oct 25 '11 at 16:05
  • 1
    I agree with @logancautrell's second suggestion, use KVC to create a dictionary of the values from the `NSManagedObject` and give that dictionary to your `CATiledLayer`. Then let the `CATiledLayer` update with that data. – Marcus S. Zarra Oct 26 '11 at 00:37
  • Wow I feel honored. I learned more about core data from your book than I did anywhere else! – logancautrell Oct 26 '11 at 02:37