Question

I've added the latest MagicalRecord develop branch code (eb72053) to a new XCode 5.0.2 Empty project w/ Core Data.

I created the following simple model for a Person:

Person Object Model

Then, in addition to creating the NSManagedObject subclass Person and adding the MagicalRecord import to my .pch file, I set my application delegate to be the following:

#import "MRFAppDelegate.h"
#import "Person.h"

@implementation MRFAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self dataTests];
    return YES;
}

- (void)dataTests {
    [MagicalRecord setupCoreDataStackWithStoreNamed:@"MagicalRecordFunTests.sqlite"];

    Person *person = [Person findFirst];
    if (!person) {
        NSLog(@"Could not find person");
        person = [Person createEntity];
    }

    NSLog(@"currentAge: %@", person.age);
    person.age = [NSNumber numberWithInteger:[person.age integerValue] + 10];
    NSLog(@"newAge: %@", person.age);

    person.name = [NSString stringWithFormat:@"%@, and now I am %@", person.name, person.age];

    [[NSManagedObjectContext defaultContext] saveToPersistentStoreAndWait];

    exit(0);
}

@end

Now, when I run this for the first time, I see the following expected output in the console:

2014-02-04 03:12:50.602 MagicalRecordFun[24299:70b] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x4ff0ac) -> Created Context UNNAMED
2014-02-04 03:12:50.604 MagicalRecordFun[24299:70b] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x4ff0ac) Set Root Saving Context: <NSManagedObjectContext: 0x8d1dc90>
2014-02-04 03:12:50.605 MagicalRecordFun[24299:70b] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x4ff0ac) Created Main Queue Context: <NSManagedObjectContext: 0x8a0e290>
2014-02-04 03:12:50.605 MagicalRecordFun[24299:70b] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x4ff0ac) Set Default Context: <NSManagedObjectContext: 0x8a0e290>
2014-02-04 03:12:50.608 MagicalRecordFun[24299:70b] Could not find person
2014-02-04 03:12:50.609 MagicalRecordFun[24299:70b] currentAge: 0
2014-02-04 03:12:50.610 MagicalRecordFun[24299:70b] newAge: 10
2014-02-04 03:12:50.610 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8a0e290) → Saving <NSManagedObjectContext (0x8a0e290): *** DEFAULT ***> on *** MAIN THREAD ***
2014-02-04 03:12:50.611 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8a0e290) → Save Parents? 1
2014-02-04 03:12:50.612 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8a0e290) → Save Synchronously? 1
2014-02-04 03:12:50.612 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8a0e290) Context DEFAULT is about to save. Obtaining permanent IDs for new 1 inserted objects
2014-02-04 03:12:50.614 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d1dc90) → Saving <NSManagedObjectContext (0x8d1dc90): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2014-02-04 03:12:50.615 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d1dc90) → Save Parents? 1
2014-02-04 03:12:50.615 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d1dc90) → Save Synchronously? 1
2014-02-04 03:12:50.616 MagicalRecordFun[24299:70b] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8d1dc90) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 1 inserted objects
2014-02-04 03:12:50.618 MagicalRecordFun[24299:70b] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke25(0x8d1dc90) → Finished saving: <NSManagedObjectContext (0x8d1dc90): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***

However, if I open up the newly created .sqlite file, I see unexpected data, which looks like each update I made to the model was called twice:

sqlite> select ZAGE,ZNAME from ZPERSON;
20|(null), and now I am 10, and now I am 20

Each time I run the app subsequently, it does the same thing, picking up the unexpected value from the database, and then adding 10 to the age on the model object, but adding 20 to the age in the database.

Was it helpful?

Solution 2

As @casademora points out, MagicalRecord is behaving correctly in this situation. The issue here is the exit(0), which causes the iOS Simulator to call application:didFinishLaunchingWithOptions a second time.

Moving this example code to main.m or changing the exit(0) to exit(1) gives the correct behavior.

OTHER TIPS

What this looks like to me is that you're running this test app over and over again and it's simply concatenating the name as you're specifying this line of code:

person.name = [NSString stringWithFormat:@"%@, and now I am %@", person.name, person.age];

Since you only have one record (one row in your sqlite data) in your data output, this seems like correct behavior to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top