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 :/.