user3408069
user3408069

Reputation:

Core Data wont save changes

I'm having a bit of a problem with my code, I'm actually using RestKit to map various objects in core data, that works well, then I need to edit certain object, when I edit it and save the context all works very well and without problem, the trouble is when I stop de application and open it again, none of the changes I've made are saved, here is my code:

[managedObjectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TaskModel.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error %@", error);

//create the managed object contexts
[managedObjectStore createManagedObjectContexts];

//Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

then this is my update method:

// Get data
    // 1.- Create the request object:
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSManagedObjectContext *localContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
    // 3.- Define the type of managed object you need:
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SavedTask" inManagedObjectContext:localContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSPredicate *searchTasktoComplete = [NSPredicate predicateWithFormat:@"taskId == %@", self.task.taskId];
    [fetchRequest setPredicate:searchTasktoComplete];
    // 5.- Execute the request:
    NSError *error;
    NSArray *fetchedTasks = [localContext executeFetchRequest:fetchRequest error:&error];

    SavedTask *taskToComplete = [fetchedTasks objectAtIndex:0];

    //complete the task in core data
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];
    NSString *date = [dateFormat stringFromDate:[NSDate date]];
    [taskToComplete setRealDate:date];
    [taskToComplete setFollowUp:@"100"];
    [localContext refreshObject:taskToComplete mergeChanges:YES];
    [localContext save:&error];
    if (![localContext save:&error]) {
        NSLog(@"error");
    }

can anyone help me?, thanks in advance

Upvotes: 0

Views: 132

Answers (2)

LLIAJLbHOu
LLIAJLbHOu

Reputation: 1313

Try this!

NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
}

Upvotes: 1

hackerinheels
hackerinheels

Reputation: 1141

You should use the saveToPersistentStore method

NSError *error = nil;
if(![localContext saveToPersistentStore:&error]){
    NSLog(@"Failed to save to data store");
}

Upvotes: 1

Related Questions