6

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!

0

3 Answers 3

12

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).

7
  • As I see, the method I wrote above seems to be deprecated, what method should I use to save in the background? There are a couple of "new" methods listed, but none of them has "background" in its name.
    – Linus
    Jan 22, 2013 at 14:09
  • @LinusAn It depends on the version of MagicalRecord that you use. You can check their github repo here (I believe saveWithBlock: is the analogous method in v3.x)
    – Alladinian
    Jan 22, 2013 at 14:15
  • Ah thank you very much! Then I'll use that one. I didn't know v3 was out yet, I better get the newest version.
    – Linus
    Jan 22, 2013 at 14:23
  • 1
    Hmm weird console log when I use saveWithBlock: -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f091a00) NO CHANGES IN ** UNNAMED ** CONTEXT - NOT SAVING
    – Linus
    Jan 22, 2013 at 14:27
  • 2
    Are you sure that you've created your entity like this: [Person MR_createInContext:localContext]; inside the block ? (Also, sorry for the v3.x thing, this is the next major version to be released. Current stable one is 2.1)
    – Alladinian
    Jan 22, 2013 at 14:43
1

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

0

Magical Record save and fetch are Context-Based. So, You can either create a record in the default context or create record in a new context using MR_createInContext method. But, while fetching the records, the context should be the same as you created.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.