1

I have Core Data model and it work fine, but I need to add new attribute. I click on my .xcdatamodel and go to Editor/Add Model Version. Now I add new attribute and add this in .h and .m file.

When I run application it give me an error :

[CubeCategory setStoreDescription:]: unrecognized selector sent to instance 0x1657bca0

Now, even if I delete application from device and instal from fresh it still give me same error.

What I am doing wrong?

EDIT :

I have set my new model as current model :

enter image description here

My model looks like :

enter image description here

And Class looks like :

.h :

@interface CubeCategory : NSManagedObject

@property (nonatomic, retain) NSNumber * categoryID;
@property (nonatomic, retain) NSNumber * position;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * storeDescription;
@property (nonatomic, retain) NSNumber * lock;
@property (nonatomic, retain) NSSet *cube;
@property (nonatomic, retain) Server *server;
@end

@interface CubeCategory (CoreDataGeneratedAccessors)

- (void)addCubeObject:(Cube *)value;
- (void)removeCubeObject:(Cube *)value;
- (void)addCube:(NSSet *)values;
- (void)removeCube:(NSSet *)values;

-(id)init:(NSManagedObjectContext*)Context;
-(void)save:(NSManagedObjectContext*)Context;

@end

and .m

@implementation CubeCategory

@dynamic categoryID;
@dynamic position;
@dynamic title;
@dynamic type;
@dynamic cube;
@dynamic server;
@dynamic lock;
@dynamic storeDescription;

-(id)init:(NSManagedObjectContext*)Context{
    self = [NSEntityDescription insertNewObjectForEntityForName:@"CubeCategory" inManagedObjectContext:Context];
    return self;
}

-(void)save:(NSManagedObjectContext*)Context{
    NSError *error;
    if (![Context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
}


@end

In my AppDelegate I have set :

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
 ...

     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
     }

EDIT 2:

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"mom"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };


    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BiViewNew.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    return __persistentStoreCoordinator;
}
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
  • 2
    Have you selected the new version as your current version after you created the new version and also make sure to add those properties to managed object subclasses if you are using NSManagedObject subclass otherwise use kvc to access the attributes on entity. – Sandeep May 13 '14 at 11:22
  • Yes I select the new version as current version – Marko Zadravec May 13 '14 at 11:39
  • Can you show the model and/or code? if `CubeCategory` is an instance it should be called `cubeCategory`. Follow naming conventions... – Lorenzo B May 13 '14 at 12:58
  • Everything looks fine. Make sure you Context is right one. That means your persistentStoreCoordinator and managedObjectModel should be set right. – HMHero May 14 '14 at 06:20
  • I am not sure what you mean by that. I edit my question and add function where I create those object. – Marko Zadravec May 14 '14 at 07:10
  • I think your problem is not relevant to the light migration between the previous verison and the new version of your model since you have mentioned that even if I delete application from device and instal from fresh it still give me same error. so your generated error unrecognized selector sent to instance (crash and debug output) at runtime logically is more about a syntax thing ([CubeCategory setStoreDescription:] must take an 'object' as a parameter whether you use it or not; and xcode does not generate any errors or warnings about this 'mistake'.) hope my answer will help you to find out – poa May 14 '14 at 07:34

3 Answers3

0

You need to create persistentStoreCoordinator with

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES],    NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

Also. Did you create updated managed object with Editor/Create NSManagedObject subClass

nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • I have set options and I have update managed object : Right click/new file/core data / NSManagedObject subclass, which I assume is the same as yours – Marko Zadravec May 13 '14 at 11:20
  • Select your Entity in data modeller and then go to menu Editor. And create object form there - "Create NSManagedObject subClass" – nerowolfe May 13 '14 at 11:23
  • 1
    Also, did you choose new model version as current for the whole data model (Identity and Type properties panel on the right) – nerowolfe May 13 '14 at 12:20
0

to ensure the light migration between the previous verison and the new version of your Core Data model you have to do the following steps :

1-Add Model Version I think you did that already 2-set the persistentStoreOptions of your UIManagedDocument as follows

   NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
    self.myAppiPhoneDatabase.persistentStoreOptions = options;

Ideally you have to set the persistentStoreOptions when you Open or create the document

poa
  • 269
  • 1
  • 4
  • 13
  • I do it like addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error ; option is your NSDictionary – Marko Zadravec May 13 '14 at 11:40
0

I've forgot to synthesize the new variable (eg: customColor, added in model NSObject and NSManagedObject in my case I'm using both) using @synthesize which makes the crash. If we are not synthesizing the variable we need setter method when accessing it, which is the root of my crash. It might help someone.

Note: In my case its not the migration problem.

arunit21
  • 670
  • 1
  • 11
  • 25