views:

75

answers:

2

Hello,

I'm getting this classic error :

The model used to open the store is incompatible with the one used to create the store

This is how it's implemented :

 NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

 NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"shotName" inManagedObjectContext:context];

NSString *newName= @"test"; 
 [newShot setName:newName];

And this is how it's designed :

alt text alt text

No only I'm getting a crash with the message above, I'm also getting this warning :

'NSManagedObject' may not respond to '-setName:'

Obviously something is wrong somewhere, I think I'm using Strings on both side though .


Edit, I'm now using this after Eimantas's comment :

 NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
 NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:@"shotName" inManagedObjectContext:context];
 NSString *newName= @"test"; 
 [newShot setValue:newName forKey:@"shotNumber"]; 
 [context saveAction];

But I`m still getting :

'NSManagedObjectContext' may not respond to '-saveAction'

+1  A: 

Use setValue:forKey:

UPDATE

NSManagedObjectContext has save method, not saveAction. So:

NSError *error = nil;
[context save:&error]
if (error) {
  [NSApp presentError:error];
  return;
}
Eimantas
Thanks for the comment, still getting an error though ( edited the question )
Julz
hmm, something might be fishy here , I'm still getting : 'NSManagedObjectContext' may not respond to '-save'
Julz
try logging the context variable. Maybe it's something else?
Eimantas
The context variable seems to be logging ok : <NSManagedObjectContext: 0x5b5bd70>
Julz
but I'm crashing with this message :'NSInvalidArgumentException', reason: '-[NSManagedObjectContext saveAction]: unrecognized selector sent to instance 0x5b5bd70'
Julz
you still have your `saveAction` somewhere? On a button maybe?
Eimantas
yep, same thing using "save" alone : 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext save]: unrecognized selector sent to instance 0x5bc27e0'
Julz
save method needs an argument (NSError object) hence selector becomes save: (note the colon).
Eimantas
+1  A: 

insertNewObjectForEntityForName:@"shotName" must be insertNewObjectForEntityForName:@"Shots". Shots is the entity name. shotName is the name of an attribute of the entity Shots. Also, like with Objective-C class names, it's standard to use singular names for your entity objects. So, Shots should be be Shot (recommended, but not required).

Also, if you change around your AppName.xcdatamodel file & generate new NSManagedObject files, you may also get the error: The model used to open the store is incompatible with the one used to create the store upon app launch. It's because it's using the old persistent store file. I call it: AppName.sqlite, but you may have a different name for this file. Search in your project for something like:

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"AppName.sqlite"]];

Then, once you know the name, to find the file, do:

find ~/Library/Application\ Support/ -name AppName.sqlite

Then, remove the file, and build & run again.

MattDiPasquale
ah :( didn`t work
Julz
Sorry, I didn't look at the code the first time. Just the error. I read the code this time, and I think I found the solution. I updated the answer.
MattDiPasquale
thanks for that, that wasn`t the only things preventing it to work :)
Julz