Save in Background

I use the Magical Record structure to save user preferences. Now, for the first time, I want to keep things in the background thread. On the Magical Record github page is an exemplary snippet that I don't quite 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 completely create a Man in a block? Thanks!

+6
source share
3 answers

Of course you can. This example simply grabs the person object from an external context (by default, one or any) and gives you a pointer to it in localContext so you can update it in the background. If you created 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 the person class (instead of MR_inContext: the instance method that is called on the person instance).

+12
source

Yes, you can create Person also in a block. The inContext: method is only necessary if, for example, you selected Person from a different context. Beware if you create Person in a block, then you should use the createInContext: method.

+1
source

Saving and retrieving magic records is context-based. Thus, you can create a record in the default context or create a record in a new context using the MR_createInContext method. But when retrieving records, the context should be the same as you created.

http://pthiaga.blogspot.in/2014/11/running-database-fetch-core-data-in.html

0
source

All Articles