1
votes

Hey I keep getting this BAD Access at the line

try managedContext.save()

The Code Runs pefectly until it try to Save my value. I think it might be becuase im using a "copy" of the Entity and not the actual Entity. But this is nessecary as you cant actaully edit the orignal model in code. So all i have to do is stop this Bad access when trying to ".save()" and im good. it is at the 4th last line of code.

So basically when using try managedContext.save(). I create a catch that will print error. so i put the "!" symbol in from of "try" so that i can see why the application throws the error and this is what i got

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store."

Picture of Error

Code:

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

var properties0 = Array<NSAttributeDescription>()

let playersList0 = NSEntityDescription.entityForName("PlayersList1", inManagedObjectContext: managedContext)

let copy = playersList0!.copy() as! NSEntityDescription

let contentTypeAttribute0 = NSAttributeDescription()
contentTypeAttribute0.name = "firstName"
contentTypeAttribute0.attributeValueClassName = "firstName"
contentTypeAttribute0.attributeType = .StringAttributeType
contentTypeAttribute0.optional = true
properties0.append(contentTypeAttribute0)


copy.properties = properties0

let playerslistCopyto = NSManagedObject(entity: copy, insertIntoManagedObjectContext: managedContext)

playerslistCopyto.setValue("John", forKey: "firstName")



do {
    try managedContext.save()
} catch {
    print("Error") //prints Error everytime
}

This prints "Error" Everytime as the catch. I basically just need to find out how to save the "copy"

3

3 Answers

1
votes

Sorry, this is an old message but it seems to not have many useful answers. I did encountered a similar error last week:

Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store." UserInfo={problemObject=<CachedWeightedRegularJourney: 0x6000023c5130> (entity: CachedWeightedRegularJourney; id: 0x6000002605e0 <x-coredata:///CachedWeightedRegularJourney/t05BA08BA-E646-44D9-8B36-713C5B4499629>; data: {
    destinationURN = "urn:trainline:generic:loc:1183";
    originURN = "urn:trainline:generic:loc:4916";
    weight = 1;
})}

The root cause in my case was that, before adding a new Entity in my model, I did not see that my model contained several configurations. So just adding the entity without adding it on the right model configuration made the save fail. What seems to happen when saving is that the coordinator is lost because it does not find on which persistent store it will save the changes.

Solution in my case: I drag and dropped the new entity to the right configuration in the model editor.

0
votes

You must have edited your model file. You need to do an auto migration. The Ray Wenderlich tutorial is adequate for that.

The NSEntityDescription you read from is not the one you're now using to try to save and Core Data is stopping you from creating an un-migrated mess. Did you edit a model name or change it's data type somewhere along the way?

0
votes

This error is given because you have a different database model that what you are currently trying to access, it most likely requires a migration. This happened because you created and saved data with a model, then edited (Added, removed or renamed for example) and then tried to access the data store without performing a migration. My recommendation is if you still haven't released the app, delete the database (Uninstall the app) and try again. Another thing you can do is tell core data to do a lightweight migration. Apple has a nice tutorial with code here