I am doing the very same thing with the same problem. My solution was to start a new cocoa project that would provide you with a checkbox for using Core Data. This will create all the ruins of Core Stack data access. The implementation is fairly straightforward from there, except that all the work is done in AppDelegate.m. The main () function is replaced by the applicationDidFinishLaunching :() method.
Only the following changes are required:
(NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FailedBankCD" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
and
(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSManagedObjectContext *context = self.managedObjectContext;
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"darn... %@", error);
exit(1);
}
NSError* err = nil;
NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Banks" ofType:@"json"];
NSArray* Banks = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"mm/dd/yy";
[Banks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" inManagedObjectContext:context];
failedBankInfo.name = [obj objectForKey:@"name"];
failedBankInfo.city = [obj objectForKey:@"city"];
failedBankInfo.state = [obj objectForKey:@"state"];
FailedBankDetails *failedBankDetails = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankDetails" inManagedObjectContext:context];
failedBankDetails.closeDate = [dateFormatter dateFromString:[obj objectForKey:@"closeDate"]];
failedBankDetails.updateDate = [NSDate date];
failedBankDetails.zip = [obj objectForKey:@"zip"];
failedBankDetails.info = failedBankInfo;
failedBankInfo.details = failedBankDetails;
NSError *error;
if (![context save:&error]) {
NSLog(@"darn... %@", [error localizedDescription]);
}
}];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects) {
NSLog(@"Name: %@", info.name);
FailedBankDetails *details = info.details;
NSLog(@"Zip: %@", details.zip);
}
}
Good luck ...
EDIT 1: To get the SQLite database that guides the change process if (! [Coordinator addPersistentStoreWithType: NSXMLStoreType configuration: nil URL: url options: nil error: & error]) {in if (! [Coordinator addPersistentStoreWithType: NSSQLiteStoreType configuration: nSSQLite URL: url options: nil error: & error]) {