0
votes

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;

    }

    }

Here are my persistent store coordinator options:

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary* options =  [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES],
                    NSMigratePersistentStoresAutomaticallyOption, 
                         [NSNumber numberWithBool:YES], 
                         NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {

}    
3

3 Answers

2
votes

When you add a new entity to your application's managed object context, you have to use the managed object context's -save: method afterwards to save those changes.

I strongly suggest reading through Apple's Core Data Programming Guide to learn about Core Data objects and methods, and how entities are created, saved, deleted and modified.

1
votes

I was totally barking up the wrong tree on this one.

What helped me find the issue was turning on core data traces with this execution parameter: -com.apple.CoreData.SQLDebug 1

Turns out I had a logic problem in choosing how to display the Visible values (I hadn't posted the code for that) which was resulting in the bug. Core data was doing the right thing and only writing changes when the values differed but I was unable to tell because I was displaying the values incorrectly.

0
votes

I suspect Alex has the right answer but can you post the code that shows you creating a NSManagedObject?

Update

Then Alex is right, you are not saving the data. Since you say you added a save routine, share that code.