3

I'm using core data to save some integer (rate) and then I call save in the context:

HeartRateBeat * beat = [HeartRateBeat heartRateWithHeartRate:rate
                            ofRecordTitle:self.recordTitle
                   inManagedObjectContext:document.managedObjectContext];

NSError * error;
[document.managedObjectContext save:&error];

Inside that convenient method I create the object using NSEntityDescription like this:

heartRateBeat = [NSEntityDescription insertNewObjectForEntityForName:@"HeartRateBeat" inManagedObjectContext:context];

(I only copied some important code, just to show what I did.)

I immediately execute a fetch request after every single heart beat inserted and managed object context saved (I save immediately), and the request shows that heart beat does appear to be stored inside Core Data (with growing array of heart beats), but if I restart my app (I'm using simulator BTW) I know things aren't actually getting saved to disk because it starts anew. Checking with SQLite3 command line shows empty tables. What am I missing here?

huggie
  • 17,587
  • 27
  • 82
  • 139

2 Answers2

4

I get the same problem but I think its just because, I assume, like me you are just stopping the app through xcode and not actually closing it down. I use this code to force a write. Im using a UIManagedDocument, shared through appdelegate, rather than setting everything up manually.

NSError *error = nil;

if (![self.managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

[[AppDelegate sharedAppDelegate].userDatabase saveToURL:[AppDelegate sharedAppDelegate].userDatabase.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];
Chris Mitchelmore
  • 5,976
  • 3
  • 25
  • 33
  • Yes I always just quit the simulator. I am calling the `[managedObjectContext save:]` on the above code on every "row" I'm inserting. But I'm not calling the UIDocument's `forSaveOperation:completionHandler:` Doesn't this write over everything if there is things already in the db? – huggie Nov 12 '12 at 08:56
  • It turns out this works. Thanks. So UIDocumentSaveForOverwriting doesn't really "overwrites"? – huggie Nov 12 '12 at 09:04
  • I have not experience any performance issues with it but my data set is not "big" – Chris Mitchelmore Nov 12 '12 at 09:09
0

I don't know about you guys, but when I want my Core Data to save, my Core Data better save.

Here's some code that will for sure save all of your Core Datas.

-(void)forceSave {
        NSManagedObjectContext * context = self.managedObjectContext; //Get your context here. 
    if (!context) {
        NSLog(@"NO CONTEXT!");
        return;
    }

    NSError * error;
    BOOL success = [context save:&error];
    if (error || !success) {
        NSLog(@"success: %@ - error: %@", success ? @"true" : @"false", error);
    }

    [context performSelectorOnMainThread:@selector(save:) withObject:nil waitUntilDone:YES];
    [context performSelector:@selector(save:) withObject:nil afterDelay:1.0];
    [context setStalenessInterval:6.0];
    while (context) {
        [context performBlock:^(){
            NSError * error;
            bool success =  [context save:&error];
            if (error || !success)
                NSLog(@"success: %@ - error: %@", success ? @"true" : @"false", error);

        }];
        context = context.parentContext;
    }

    NSLog(@"successful save!");

}

Note that this is BAD CODE. Among other problems, it's not thread-safe and takes too long. However, try using this and deleting some parts of it to your satisfaction :)

Clay Schubiner
  • 133
  • 1
  • 4