Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magical Record save in background

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!

like image 688
Linus Avatar asked Jan 22 '13 13:01

Linus


1 Answers

Of course you can. This example just grabs a person object from the outer context (your default one or whatever) and gives you a pointer to it in the localContext so you can update it in the background. If you were to create a person from scratch you could do something like this:

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){

    Person *localPerson = [Person MR_createInContext:localContext];

    localPerson.firstName = @"John";
    localPerson.lastName = @"Appleseed";

}];

And you're done.

PS. Note that MR_createInContext: is a class method called on Person class (instead of MR_inContext: instance method which is called on person instance).

like image 192
Alladinian Avatar answered Sep 19 '22 01:09

Alladinian