I’m using the Magical Record framework to save user settings. Now, for the first time, I want to save things in a background thread. On Magical Record’s github page is an example snippet I don’t fully understand:
Person *person = ...;
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
Person *localPerson = [person MR_inContext:localContext];
localPerson.firstName = @"John";
localPerson.lastName = @"Appleseed";
}];
Why is the first line needed? Can’t I just completely create the Person in the block? Thank you!
Of course you can. This example just grabs a
personobject from the outer context (your default one or whatever) and gives you a pointer to it in thelocalContextso you can update it in the background. If you were to create apersonfrom scratch you could do something like this:And you’re done.
PS. Note that
MR_createInContext:is a class method called onPersonclass (instead ofMR_inContext:instance method which is called onpersoninstance).