0

I am trying to make my app save to core data in the AppDelegate.swift but upon quitting the app, it crashes with this log in the console:

2015-04-04 22:04:38.043 myapp[14882:714850] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/kiancross/Library/Developer/CoreSimulator/Devices/59D353AF-8C56-42D6-8B35-9F51FE04D1BC/data/Containers/Data/Application/66D76678-B819-4B01-8172-6324BB5D2E7B/Documents/myappsqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7fc453dcb120 {metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
    metadata =     {
        NSPersistenceFrameworkVersion = 519;
        NSStoreModelVersionHashes =         {
            SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
2015-04-04 22:04:38.047 myapp[14882:714850] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x7fc453dcce00 {NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7fc453db2360 "The operation couldn’t be completed. (Cocoa error 134100.)"}), Optional([NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7fc453dcb120 {metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        SavedData = <f13dc99c 2ebf5dd2 a89322cb 537a0f94 361477df 04077315 5954c0b5 afd3f9a1>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "E748E18D-F898-4AC1-96A4-7F8F1925D369";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}])

The code that is triggering this error in my AppDelegate.swift is:

func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        let entity =  NSEntityDescription.entityForName("SavedData", inManagedObjectContext: managedContext)


        let managedObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)

        managedObject.setValue(sharedData.jsonUrl, forKey: "jsonUrl")

        var error: NSError?
        if !managedContext.save(&error) {
            println("Could not save \(error), \(error?.userInfo)")
        }

    }

This is in my CoreData model; you can see a screen shot below:

core data screen shot

As you can see it has everything it should have in it.

My question is what part of my code is causing this crash? I followed this tutorial on implementing CoreData into applications and I'm almost certain I've followed it as exactly as possible to fit my apps needs.

Thank you,

cross
  • 363
  • 1
  • 4
  • 15
  • 1
    See [this](http://stackoverflow.com/q/8078337/3985749) question/answer. Simplest fix is just to delete the app and rerun. – pbasdf Apr 04 '15 at 21:46

1 Answers1

1

I'm not very sure but the last line of your error says: reason=The model used to open the store is incompatible with the one used to create the store}]) which means the datamodel of coredata is not the same as the one you're using.

Simply delete the app (LongPress on the app icon and delete it) and run it again and that would solve that problem at least.

A better explanation is here

Remove the app from the simulator and perform a clean on your project. That should clear those issues up. Make sure that you are not running in the debugger when you delete the app or else it won't actually delete it properly.

If you want to be sure its gone, check this directory Users/INSERT_YOUR_USER_HERE/Library/Application Support/iPhone Simulator/ for your app's folder, under the version you're running.

Note: This is for development only. For production, you need to implement some sort of migration. Google "Core Data Migration", with lightweight migration being the simplest."

Community
  • 1
  • 1
Henny Lee
  • 2,970
  • 3
  • 20
  • 37