Home > Enterprise >  Changing an attribute in CoreData gives me NSCocoaErrorDomain Code=134140
Changing an attribute in CoreData gives me NSCocoaErrorDomain Code=134140

Time:06-10

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:

  1. Keep the attribute, street_no, I want to change as it was originally defined (string)
  2. Create a new attribute called, street_no_2, and define it as an Int64
  3. Delete the two class files for the entity containing the attribute
  4. Regenerate the class files for the entity manually from the Editor menu
  5. Clean Project Folder
  6. Build & Run
  7. Delete the original attribute, street_no
  8. Delete the two class files for the entity containing the attribute
  9. Regenerate the class files for the entity manually from the Editor menu
  10. Clean Project Folder
  11. Build & Run
  12. Rename street_no_2 to street_no
  13. Delete the two class files for the entity containing the attribute
  14. Regenerate the class files for the entity manually from the Editor menu
  15. Clean Project Folder
  16. 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.

  • Related