Code needed to save to a CoreData store
Continuing on my quick CoreData excursion, here is what I figured out and learned today regarding actually getting your NSManagedObjects to save to a real file. This assumes the previous post's code.
Saving your objects to the file/store is pretty straight forward. Again, I'm just trying to get basic functionality working. Once I have that done I can expand on my knowledge and what I try to code after I understand what is going on.
Saving is pretty straight forward once you have the setup code done. In the previous entry's code example, there is a comment you need to remove and implement:
//- (NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:nil URL:(NSURL *)storeURL options:(NSDictionary *)options error:(NSError **)error // this is what you use to set up the database type, etc. In this snippet I don't worry about it because I just want it to compile and display something.
This is my brute force code:
NSError *error;
// Build an NSURL to our physical database file.NSFileManager *fileManager = [NSFileManager defaultManager];NSString *folder = @"~/Library/Application Support/Tiny±Git/";folder = [folder stringByExpandingTildeInPath];if ([fileManager fileExistsAtPath: folder] == NO){[fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:error];}
NSURL *fileURL = [NSURL fileURLWithPath: [folder stringByAppendingPathComponent: @"TinyGit.sqlite"]];// Now tell our NSPersistanceStoreCoordinator the type of DB you want (I'm using SQLite, you can also use XML, etc.)// Pass in our fileURL NSURL which is where the database will be strored.// There are also configurations, and options, but we can ignore this--I'll worry about those once I get this working.[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:error];
Once you have the store coordinator set up this way, then all you have to do to store your NSManagedObject(s) is call:
[newContext save:error];
after you have created them and added them to the NSManagedObjectContext. The interesting thing here is that the the save: actually forces all the objects within the context to be saved all at once. It's all or nothing. So make sure you only add the objects to the context that you want to end up in the database.
Just to verify that this actually did work, I went to my Application Support folder and sure enough there was a new Tiny±Git folder and within it a new file TinyGit.sqlite. Just to see what got stored (if anything) I opened it up with SQLiteManager:
Compare this with the actual data model:

So, it looks like CoreData builds lots of infrastructure. Just to see what data got stored I drilled into the data:
Well at least the data that I specified for my simple example in the previous post got saved properly, but there sure is a lot of extraneous stuff going on under the covers.
What do I conclude from all this? I honestly am not sure. I'm feeling a bit more confident after seeing how relatively easy it is (although limiting) to save a context. I'll have to see what it's like to actually retrieve/fetch data using CoreData.




