Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

What is the replacement method for this MagicalRecord deprecated call?

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...