Question

I have a project that manages a database with Core Data. This DB works fine, but I need to add a new field in a table. When I create a new version of the data model, add the field and back to create the NSManagedObject subclasses (autogenerated). the application finishes with an error about "unrecognized selector" in the new field that I added.

The new autogenerated table is the next:

File.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Session;

@interface File : NSManagedObject

@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * fileType;
@property (nonatomic, retain) NSString * pathFile;
@property (nonatomic, retain) NSString * name;     // NEW FIELD
@property (nonatomic, retain) Session *session;

@end

File.m

#import "File.h"
#import "Session.h"

@implementation File

@dynamic date;
@dynamic fileType;
@dynamic pathFile;
@dynamic name;      // NEW FIELD
@dynamic session;

@end

These are all the relationships in the database:

Database

And this is the creation of the entity in the code:

File *newFile = [File createEntity];
newFile.pathFile = pathFile;
newFile.fileType = @"Video";
newFile.name = @"Video 1";
newFile.date = [[NSDate alloc] init];
if ([sessionPicker selected] < sessions.count) {
     newFile.session = [sessions objectAtIndex:[sessionPicker selected]];
}

[[NSManagedObjectContext defaultContext]saveToPersistentStoreWithCompletion:nil];

And the error that I mentioned before is the next (only fails in the new field, if I comment the line to add the name of the file, the creation works fine):

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[File setName:]: unrecognized selector sent to instance 0xc667770'

I tried to remove the references to datamodel and added them again, but this fails too. Also, I tried to clean the project and remove the app of the simulator, but this fails too.

Any solution or suggestion, please?

Edit:

NSLog output

(lldb) po [[newFile entity] properties]
<__NSArrayM 0xc6f7340>(
(<NSAttributeDescription: 0xc2ac350>), name date, isOptional 1, isTransient 0, entity File, renamingIdentifier date, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null),
(<NSAttributeDescription: 0xc2ac3a0>), name fileType, isOptional 0, isTransient 0, entity File, renamingIdentifier fileType, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null),
(<NSAttributeDescription: 0xc2ac3f0>), name pathFile, isOptional 0, isTransient 0, entity File, renamingIdentifier pathFile, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null),
(<NSAttributeDescription: 0xc2ac440>), name syncID, isOptional 1, isTransient 0, entity File, renamingIdentifier syncID, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null),
(<NSRelationshipDescription: 0xc2acc50>), name session, isOptional 1, isTransient 0, entity File, renamingIdentifier session, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, destination entity Session, inverseRelationship files, minCount 0, maxCount 1, isOrdered 0, deleteRule 1
)
Was it helpful?

Solution

Finally, I achieved solve the problem: in the "Build phases" (in the properties of the project), I delete a copy of the bundle of the DB and back to added.

The problem was that this bundle was generated in the Derived Data folder, and although I will create new copies, this file is keeping deprecated. When I deleted all files and folders of Derived Data, xCode shows me several errors about this folder and related to the bundle. So, I build the datamodel again, copy the new bundle in the project, and add again (after delete the old bundle) in the "Build phases" of the project.

OTHER TIPS

You cannot simply upgrade the Database schema in Core Data without migrating it. This provides a "mapping" for the app to follow. Read through this Apple Doc for more info, and instructions on how to migrate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top