magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

MR_contextForCurrentThread deprecated #1150

Open tlupo opened 8 years ago

tlupo commented 8 years ago

A background thread treatment (via dispatch_async) in my application needs to use Core Data objects fetched from the main thread (these objects are loaded one time in the main thread because this table contains a large number of rows).

Sometimes an error occurred when I try to access an attribute of one of these Core Data objects:

Crashed: com.apple.root.default-qos EXC_BREAKPOINT

or

Fatal Exception: NSInvalidArgumentException -[Zone isTemporaryID]: unrecognized selector sent to instance

It's probably because I try to access a Core Data object loaded from the main thread in another thread.

When I have to save an object, I use the context provided by methods like [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) to reload this object ([myObject MR_inContext:). But in this case I don't have to open a save block and I need a context for this thread. I would like to use MR_contextForCurrentThread method but it seems deprecated.

Is there any risk to use this method ? Or is there a better way to access Core Data objects in my background thread without fetching again ?

Thank you very much !

tonyarnold commented 8 years ago

Can you explicitly hold on to a context while interacting with your background thread? If you can, just ensure you keep using performBlockAndWait: (or the async variant) when you need to interact with your fetched objects. That will ensure that the work that needs to be done will be queued serially on the correct thread.

Please don't use MR_contextForCurrentThread — it's removed in MR 3, and won't be coming back.