I have a mapping model problem in CoreData with Xcode 13.4 and Swift 5.
Originally when I created the entities in CoreData, one of them had an attribute that was defined as
street_no String
Once I realized that I meant to define it as an Int64, I went and changed it, deleted all the two class files, regenerated the code, and my app would always crash with the following error:
[error] error: addPersistentStoreWithType:configuration:URL:options:error: returned error NSCocoaErrorDomain (134140)
in the AppDelegate function:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: “Invoice_Gen")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
If I changed the attribute back to String, everything worked fine.
After much Googling, the long winded solution I found was:
- Keep the attribute, street_no, I want to change as it was originally defined (string)
- Create a new attribute called, street_no_2, and define it as an Int64
- Delete the two class files for the entity containing the attribute
- Regenerate the class files for the entity manually from the Editor menu
- Clean Project Folder
- Build & Run
- Delete the original attribute, street_no
- Delete the two class files for the entity containing the attribute
- Regenerate the class files for the entity manually from the Editor menu
- Clean Project Folder
- Build & Run
- Rename street_no_2 to street_no
- Delete the two class files for the entity containing the attribute
- Regenerate the class files for the entity manually from the Editor menu
- Clean Project Folder
- Build & Run
While I am sure the solution above can be shortened, obviously the problem has to do with the mapping model.
Surely there must be a way to just change an attribute's type?
CodePudding user response:
The error comes because of a mismatch between the stored database file, where you have a string, and what you've told Core Data is in there, which is now an Int. You refer to a mapping model problem but it doesn't sound like you've used a mapping model at all, just changed the model definition directly and expected the framework to cope.
If this is an early stage app (i.e you're still developing the first version) just delete the app from the device or simulator and re-run once you've changed the model and class definition files.
If your app is out in the wild and on user devices, and you don't care about keeping the data, you'll need to include some code to delete the database file on startup according to some defaults key that you define.
If you want to keep the data, then you need to create a new model version in the core data model editor, where the type is defined how you like it, and then add migration or mapping rules to define what should happen during the conversion - I don't think there is any automatically inferrable mapping rule to turn any string into an integer.