简体   繁体   中英

Swift: CoreData – managedObjectContext-Error

I'm having a bug in my App after I wanted to implement CoreData. I hope you guys can help me.

When my App tries to use the function addNewContact (which is the case for testing in viewDidLoad()) it crashes. I was able to track the error to the default AppDelegate class, because every time i try to declare my managedContext, the print("2")-statement is not executed, but the print("1")-statement right before i declare the managedContext is.

Firstly here is my function's code:

//MARK: - Add contact to CoreData
func addNewContact()
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    print("1")
    let managedContext = appDelegate.managedObjectContext
    print("2")

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

    let contact = Contact(entity: entity!, insertIntoManagedObjectContext: managedContext)
    contact.firstName = "testName"
    contact.lastName = "testLastName"
    contact.eMail = "testMail"
    contact.number = "testNumber"

    do {
        try managedContext.save()
        print("contact \(contact.firstName, contact.lastName) saved succesfully")
    } catch {
        fatalError("Error while trying to save \(contact.firstName, contact.firstName)")
    }
}

Here is now the default AppDelegate.swift with the error: AppDelegate.swift with error

I really have no clue what I am doing wrong. I hope you can help me. Thank you so much in advance.

BTW Here is the code to my CoreData-Entity Contact :

class Contact: NSManagedObject
{

// Insert code here to add functionality to your managed object     subclass
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)
{
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}
}

and

import Foundation
import CoreData

extension Contact {

@NSManaged var firstName: String?
@NSManaged var number: String?
@NSManaged var eMail: String?
@NSManaged var lastName: String?

}

EDIT: And my console output:

1
2016-02-04 01:02:03.370 Lime[8326:9552995] CoreData: error: -        addPersistentStoreWithType:SQLite configuration:(null)    URL:file:///Users/Raphael/Library/Developer/CoreSimulator/Devices/CCAE776D-    A906-4D46-BBE2-9DC3E4E70F01/data/Containers/Data/Application/CDC77942-    8076-459E-9903-AEA97BEE6BA4/Documents/SingleViewCoreData.sqlite options:  (null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "   (null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f   b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_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 = 641;
        NSStoreModelVersionHashes =         {
            Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the  one used to create the store";
}
2016-02-04 01:02:03.373 Lime[8326:9552995] Unresolved error Error     Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's    saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x7b6f5430 {Error   Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one   used to create the store}}, NSLocalizedFailureReason=There was an error   creating or loading the application's saved data.},     [NSLocalizedDescription: Failed to initialize the application's saved data,     NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "(null)"     UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one    used to create the store}, NSLocalizedFailureReason: There was an error   creating or loading the application's saved data.]
(lldb) 

somehow it won't get displayed as code. Tried multiple times. Sorry :/.

The important error is the one at the top of your console output which in part says...

 NSCocoaErrorDomain Code=134100

This error is telling you that the model has been changed and is therefore now incompatible with the store. There are two ways to fix this:

  1. The easy way is to just delete the app from the device. Therefore there will be no store to cause an incompatibility when you run it again. This is what you do (a lot) while developing a core data app as the model is being developed.
  2. The hard way, that you will need to do if you distribute the app, is to perform a migration to upgrade the store from the old model to the new.

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