What is the replacement method for this MagicalRecord deprecated call?
Asked Answered
M

2

12

How do I find the replacement method in MagicalRecord for this (which has been deprecated)? I have looked at Google, SO and the docs; nothing seems to be a replacement, and of course, nothing in the docs tell you what replaced the deprecated method. :-{

[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveErrorHandler:^(NSError *error)
Morette answered 12/12, 2013 at 5:1 Comment(0)
M
23

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.

Micahmicawber answered 12/12, 2013 at 6:16 Comment(9)
So what do I replace MR_saveErrorHandler: with?Morette
Anyone of the following: MR_saveOnlySelfAndWait, MR_saveOnlySelfCompletion:, MR_saveToPersistentStoreAndWait, MR_saveToPersistentStoreCompletion:Micahmicawber
Thank you very much... on a different subject, I like reading docs in a PDF... would you be interested in me creating a PDF of the current files in the Docs folder? I would like to "pay it forward" so to speak.. :DMorette
If you can make it a script so docs can be updated, I suppose. However, I think a docset would probably be more useful. Have you looked at the generated doc pages for MagicalRecord on Cocoadocs? We're slowly adding header docs to make this better compatible with this project. And it should be more up to date than even generated PDFs.Micahmicawber
What's the way to fetch in a different thread/context? I'm doing it in the saveWithBlock: block since it seems ok there. Is there perhaps a better way?Squabble
MR_contextForCurrentThread is deprecated. What to use instead?Proximity
The link to the blog post is dead. The correct one should be here.Ahern
Since MR_contextForCurrentThread is deprecated now then we can't use the default MagicalRecord methods, I mean where we don't pass the context where we're working with. So we should always pass the context where we're working on.Disharmony
saveWithBlock doesn't explain how to retrieve entities from the correct MOC. IE how to I get an entity from the current thread?Naoma
S
1

casademora is correct but I'll try to be more precise because I encounter some issues when converting my

    [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreWithCompletion:nil];

into

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)

You have to be careful to change or create your entities inside the block but it's not enough.

To retrieve your entities you must use selection request with context too.

MR_findFirstByAttribute:withValue

is not enough and updates won't be saved. You have to use instead

MR_findFirstByAttribute:withValue:InContext:localContext

And when creating entity, it's the same

MR_createEntity

must be change to

MR_createEntityInContext:localContext

Then it works like a charm :)

Sport answered 28/5, 2016 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.