Magical Record not saving
Asked Answered
D

4

8

I am using Magical Record: https://github.com/magicalpanda/MagicalRecord

I am trying to save records that I get the from my website to the sqllite database using Magical Record and Core Data, but I keep getting the error:

MR_saveWithErrorCallback:](0xaaa6bd0) NO CHANGES IN CONTEXT <NSManagedObjectContext (0xaaa6bd0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD *** - NOT SAVING

Here is the code:

             for(int i = 0; i < count; i += 1)
             {
                 // results  = array of all services from site
                 NSDictionary * result = [results objectAtIndex: i];

                 NSNumber * sid = @([[result objectForKey: @"id"] intValue]);
                 NSNumber * parent = @([[result objectForKey: @"parent"] intValue]);
                 Service * service  = [Service createEntity];

                 NSString * image = [NSString stringWithFormat: @"%@", [result objectForKey: @"image"]];

                 NSString * name  = [NSString stringWithFormat: @"%@", [result objectForKey: @"name"]];
                 NSString * machine_name  = [NSString stringWithFormat: @"%@", [result objectForKey: @"machine_name"]];


                 [service setDate: [NSDate date]];
                 [service setSid: sid];
                 [service setName: name];

                 [service setImage: image];
                 [service setParent: parent];
                 [service setMachine_name: machine_name];


                 [[NSManagedObjectContext defaultContext] saveNestedContexts];
Doctorate answered 7/12, 2012 at 17:24 Comment(0)
A
7

The issue is that you are not saving the right context. If you look at the source, [Service createEntity] creates the ManagedObject in the context for the current thread. Not in the defaultContext.

So what you need to do is, instead of [[NSManagedObjectContext defaultContext] saveNestedContexts], you should be saving the context for the current thread (i.e the context which the ManagedObject was created in). So the code should be [[NSManagedObjectContext MR_contextForCurrentThread]

Ailina answered 17/12, 2012 at 23:59 Comment(0)
S
5

Have you tried using the saveInBackgroundWithBlock: utility method?

If not you might want to try something like the code below. Note that it'll save all your objects in one go in the background and then perform the completion block on the main thread:

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
    Service *service = [Service createInContext:localContext];

    ... set values ...
} completion:^{
    ...
}];

Other than that, you can see if [[NSManagedObjectContext defaultContext] hasChanges] returns YES to check if it's an issue with the saving or the entity itself.

Subtropics answered 8/12, 2012 at 15:19 Comment(0)
S
1

In my swift project I do:

MagicalRecord.setupCoreDataStack()

So then I need to do the following to save to disk:

NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
Stillas answered 3/2, 2016 at 22:23 Comment(0)
I
0

move the line

[[NSManagedObjectContext defaultContext] saveNestedContexts];

outside the for loop , it worked for me

Insolent answered 24/2, 2013 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.