Josue Espinosa
Josue Espinosa

Reputation: 129

Table updates with new content, but Core Data doesnt

I have a table view of all my entities, when one is selected, it brings up a detail view. In the detail view there is an edit barbuttonitem. When selected, the code below is run. The table view reloads on viewdidappear, and detects the change in first name, but when I close the simulator and open it again, it shows the previous value. Am I missing something?

-(void)doneEditing{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:[NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext]];
    NSError *error = nil;
    NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"first == %@", _athleteFirst];
    [request setPredicate:predicate];

    Athlete *currentAthlete = [results objectAtIndex:0];
    currentAthlete.first = _firstDetailTextField.text;

    [self.navigationItem setRightBarButtonItem:nil animated:YES];
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    UIBarButtonItem *allAthletesButton=[[UIBarButtonItem alloc] initWithTitle:@"All Athletes" style:UIBarButtonItemStylePlain target:self action:@selector(popACapInYoViewController)];
    UIBarButtonItem *editButton=[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleDone target:self action:@selector(editAthlete)];
    [self.navigationItem setRightBarButtonItem:editButton animated:YES];
    [self.navigationItem setLeftBarButtonItem:allAthletesButton animated:YES];
    [self.view endEditing:YES];
}

Upvotes: 0

Views: 40

Answers (1)

Martin R
Martin R

Reputation: 540005

You have to save the managed object context, so that changes are written to the persistent store and therefore permanent:

NSError *error;
BOOL success = [context save:&error];
if (!success) {
    NSLog(@"could not save: %@", [error localizedDescription]);
}

Upvotes: 1

Related Questions