Snowman
Snowman

Reputation: 32081

Understanding Core Data saving

I'm following this Core Data tutorial and one thing is confusing me. Let's say I do:

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *failedBankInfo = [NSEntityDescription
    insertNewObjectForEntityForName:@"FailedBankInfo" 
    inManagedObjectContext:context];
[failedBankInfo setValue:@"Test Bank" forKey:@"name"];
[failedBankInfo setValue:@"Testville" forKey:@"city"];
[failedBankInfo setValue:@"Testland" forKey:@"state"];
NSManagedObject *failedBankDetails = [NSEntityDescription
    insertNewObjectForEntityForName:@"FailedBankDetails" 
    inManagedObjectContext:context];

NSError *error;
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

I've read that CoreData is not actually intended for storing data, but for managing it, and the backing store of CoreData is SQLite. So what happens to this data that I've added above when the app session terminates? Does the data automatically get saved to some SQLite file anytime I run code like the above? Or is Core Data empty every time I start the app, unless I follow some specific actions that fill it up?

Upvotes: 0

Views: 1187

Answers (1)

FluffulousChimp
FluffulousChimp

Reputation: 9185

The save: method on NSManagedObjectContext commits unsaved changes to the persistent store. (By the way, the persistent store type may be sqlite; but the are other persistent store types as well.) In the example code, the changes you make are persisted to the persistent store.

Core Data is a object persistence framework - so in that sense it does save data. It persists the object graph encapsulated in the managed object model.

Upvotes: 1

Related Questions