Why is Core Data not persisting these changes to disk?

Posted by scott on Stack Overflow See other posts from Stack Overflow or by scott
Published on 2010-05-03T07:41:39Z Indexed on 2010/05/03 7:48 UTC
Read the original article Hit count: 282

Filed under:
|

I added a new entity to my model and it loads fine but no changes made in memory get persisted to disk. My values set on the car object work fine in memory but aren't getting persisted to disk on hitting the home button (in simulator). I am using almost exactly the same code on another entity in my application and its values persist to disk fine (core data -> sqlite3); Does anyone have a clue what I'm overlooking here? Car is the managed object, cars in an NSMutableArray of car objects and Car is the entity and Visible is the attribute on the entity which I am trying to set. Thanks for you assistance. Scott

- (void)viewDidLoad {
   myAppDelegate* appDelegate = (myAppDelegate*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext* managedObjectContex = appDelegate.managedObjectContext;
    NSFetchRequest* request = [[NSFetchRequest alloc] init];
    NSEntityDescription* entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:managedObjectContex];
    [request setEntity:entity];

    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [request setSortDescriptors:sortDescriptors];

    [sortDescriptors release];
    [sortDescriptor release];

    NSError* error = nil;
    cars = [[managedObjectContex executeFetchRequest:request error:&error] mutableCopy];

    if (cars == nil) {
        NSLog(@"Can't load the Cars data! Error: %@, %@", error, [error userInfo]);
    }


    [request release];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    Car* car = [cars objectAtIndex:indexPath.row];

    if (car.Visible == [NSNumber numberWithBool:YES]) {
        car.Visible = [NSNumber numberWithBool:NO];
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        car.Visible = [NSNumber numberWithBool:YES];
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    }

    }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-data