I'm trying to do a lightweight migration. I made a new project following step by step this tutorial and I get things work. But now I'm in another app and everything seems go well but when I try to access to the new field then I get the following error.
[Promocion setEnlace:]: unrecognized selector sent to instance 0x7fafe40bceb0
I have selected the correct model.
This is the model Promocion.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
/*
@interface Promocion : NSManagedObject
@property (nonatomic, retain) NSNumber * id_promocion;
@property (nonatomic, retain) NSString * descripcion;
@property (nonatomic, retain) NSString * titulo;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) NSString * fecha_inicio;
@property (nonatomic, retain) NSString * fecha_fin;
@end
*/
NS_ASSUME_NONNULL_BEGIN
@interface Promocion : NSManagedObject
// Insert code here to declare functionality of your managed object subclass
//+(id)personaWithContext:(NSManagedObjectContext *)context;
@end
NS_ASSUME_NONNULL_END
#import "Promocion+CoreDataProperties.h"
Promocion.m
#import "Promocion.h"
@implementation Promocion
- (NSComparisonResult)compare:(Promocion *)otherObject {
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"MMMM dd,eeee HH:mm a z"];
NSDate *dateA = [dateformat dateFromString:self.fecha_inicio];
NSDate *dateB = [dateformat dateFromString:otherObject.fecha_inicio];
return [dateA compare:dateB];
}
@end
And the following were auto-generated files Promocion+CoreDataProperties.h
#import "Promocion.h"
NS_ASSUME_NONNULL_BEGIN
@interface Promocion (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *descripcion;
@property (nullable, nonatomic, retain) NSString *fecha_fin;
@property (nullable, nonatomic, retain) NSString *fecha_inicio;
@property (nullable, nonatomic, retain) NSNumber *id_promocion;
@property (nullable, nonatomic, retain) NSString *titulo;
@property (nullable, nonatomic, retain) NSString *url;
@property (nullable, nonatomic, retain) NSString *enlace;
@end
NS_ASSUME_NONNULL_END
Promocion+CoreDataProperties.m #import "Promocion+CoreDataProperties.h"
@implementation Promocion (CoreDataProperties)
@dynamic descripcion;
@dynamic fecha_fin;
@dynamic fecha_inicio;
@dynamic id_promocion;
@dynamic titulo;
@dynamic url;
@dynamic enlace;
@end
AppDelegate.m
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"data.sqlite"];
//Migration
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption :@YES,
NSInferMappingModelAutomaticallyOption :@YES
};
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
//options:nil
options:options//automatic migration
error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
return _persistentStoreCoordinator;
}
Additional info
I have updated the version of the project.
Where I access the field called "enlace"
NSString* className = NSStringFromClass([Promocion class]);
NSEntityDescription *entity = [NSEntityDescription entityForName:className inManagedObjectContext:context];
Promocion* obj = (Promocion*)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
obj.id_promocion = [NSNumber numberWithInt:[[centro objectForKey:@"id_promo"] intValue]];
obj.titulo = [centro objectForKey:@"titulo"];
obj.descripcion = [centro objectForKey:@"descripcion"];
obj.url = [centro objectForKey:@"imagen"];
obj.fecha_inicio = [centro objectForKey:@"fecha_ini"];
obj.fecha_fin = [centro objectForKey:@"fecha_fin"];
obj.enlace = [centro objectForKey:@"enlace"];
What I've tried
I have changed Promocion.h to avoid use Promocion+CoreDataProperties.h but I still have the same error.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Promocion : NSManagedObject
@property (nonatomic, retain) NSNumber * id_promocion;
@property (nonatomic, retain) NSString * descripcion;
@property (nonatomic, retain) NSString * titulo;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) NSString * fecha_inicio;
@property (nonatomic, retain) NSString * fecha_fin;
@property (nonatomic, retain) NSString * enlace;
@end
Another attempt
I recreate Promocion.h deleting the old one and creating a new one using Editor->Create NSOjecteManaged subclass. It looks like is working. But when I rollback using git to a last not updated core data commit and install it on a device and then upgrading the app with the last commit with new coredata model then start failing again. Damn I feel lot of frustration.
Partial Appdelegate.m
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
_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
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"data.sqlite"];
//Migration
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption :@YES,
NSInferMappingModelAutomaticallyOption :@YES
};
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
//options:nil
options:options//automatic migration
error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
return _persistentStoreCoordinator;
}
Solution
What finally was going wrong is that I had two references(dunno why) of Coredata file. One of them was still on first version. I have changed that version and everything works as expected.