To add CoreData functionality to an existing project (maybe is just an improvement to cache some kind of information) just to this few steps.

  • Add the CoreData framework to your project
  • Create a new CoreData Model file
  • Fill your model with entities you need
  • Modify AppDelegate adding the following code
@interface AppDelegate : UIResponder <UIApplicationDelegate>  
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (NSString *)applicationDocumentsDirectory;
#pragma mark - CoreData 

//Explicitly write Core Data accessors
- (NSManagedObjectContext *) managedObjectContext {
    if (managedObjectContext != nil) {
        return managedObjectContext;
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];

    return managedObjectContext;

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil] ;

    return managedObjectModel;

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                               stringByAppendingPathComponent: @"DiasFestivos.sqlite"]];
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                  initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil URL:storeUrl options:nil error:&error]) {
        NSLog(@"Error creating persistentStoreCoordinator: %@" , [error localizedDescription]);

    return persistentStoreCoordinator;

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  •  Finally add your retrieve/store logic. In my case I have a Loader class
-(void) loadUsingCoreData: (DiasFestivos*) model  
         WithTarget: (id)target  
         WithOnComplete: (SEL) onComplete
         WithOnError: (SEL) onError
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    NSError *error;

    NSMutableArray *data = [[NSMutableArray alloc] init];

    NSArray *fetchedObjects = [coredata_ctx executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *info in fetchedObjects) {
        NSNumber* dia = [info valueForKey: @"dia"];
        NSNumber* mes = [info valueForKey: @"mes"];
        NSNumber* ano = [info valueForKey: @"ano"];
        NSString *nombre = [info valueForKey: @"nombre"];
        NSString *dia_semana = [info valueForKey: @"dia_semana"];   

        [self CreateEntity:data 
                   WithDia: [dia intValue]
                   WithMes: [mes intValue]
                   WithAno: [ano intValue]

    } = data;

-(void) saveToCoreDataWithDia:(NSInteger) dia 
                      WithMes: (NSInteger) mes
                      WithAno: (NSInteger) ano 
                     WithName: (NSString*) nombre
                WithDiaSemana: (NSString*) diaSemana 
    NSManagedObject *dia_festivo = [NSEntityDescription
    [dia_festivo setValue:[NSNumber numberWithInt:dia] forKey:@"dia"];
    [dia_festivo setValue:[NSNumber numberWithInt:mes] forKey:@"mes"];
    [dia_festivo setValue:[NSNumber numberWithInt:ano] forKey:@"ano"];
    [dia_festivo setValue:nombre  forKey:@"nombre"];
    [dia_festivo setValue:diaSemana forKey:@"dia_semana"];

    NSError *error;
    if (![coredata_ctx save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
  You can find the complete example here


  1. Wires are Obsolete: Adding Core Data Existing iPhone Projects