简体   繁体   中英

Magical Record, saving objects

I use MR in my iOS App. I use it in the same way as in my previous apps, but my objects are not saved in database.

I see from debug that Magical Record finds *.sqlite database correctly in setupAutoMigratingCoreDataStack method

Here is some sample test code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MagicalRecord setupAutoMigratingCoreDataStack];

    NSManagedObjectContext *sampleContext = [NSManagedObjectContext MR_defaultContext];

    NSLog(@"hasChanges: %d", [sampleContext hasChanges]);
    self.entity = [MyEntity MR_createEntityInContext:sampleContext];
    NSLog(@"hasChanges: %d", [sampleContext hasChanges]);

    [sampleContext MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
        NSLog(@"save: %d %@", success, error);
    }];

    return YES;
}

Here is output:

 hasChanges: 0
 hasChanges: 1
 save: 1 (null)

I reopen the app see that no entities are saved in local db ( [MyEntity MR_findAll] ) , also I open *.sqlite db and see that ZMyEntity table is empty.

What can be the reason of such behavior?

Thanks in advance!

Use the MR_saveToPersistentStore* variant if you want your changes to be pushed down to the store. MR uses nested contexts, and as such, you have the option to save only one level, or save to the store. We made this an explicit code choice to help you decide which option you want without writing all the boilerplate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM