What is the replacement method for this MagicalRecord deprecated call?

The deprecated method in question is:

[NSManagedObjectContext MR_contextForCurrentThread]

I did write a little blog post about this a while ago, though I admit it is on my personal blog, and not in any official docs. But, TL;DR, the bottom line is, in the world of GCD and queues, you cannot guarantee a 1-1 mapping of a queue to a thread, despite GCD being run on threads. The way to make sure things work going forward for you is using the following pattern:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    //make your changes in the localContext
}];

This solves the subtle cross thread issues that crop up in contextForCurrentThread by simply enforcing the rule that you should do all work in a different thread in a thread specific context. By creating a new context every time you save, and not re-using the context, you will guarantee to not cross threads, and to not crash your app 1% of the time.

Leave a Comment