I have an OSX application that reads a JSON file and inserts rows into a SQLite database using Core Data. My problem is that not every row in the JSON file is loaded even though the application reports all rows written.
It's always the last few hundred rows or so that are not written which leads me to believe I'm not closing/flushing the last writes to the database.
I don't see this function in examples or on this site. Here is the code:
@autoreleasepool {
NSManagedObjectContext *context = managedObjectContext();
NSArray *gemList;
//
// Get ready to read the JSON FILE
//
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"/JSONTesting/I10_CM_TO_I9_CM.JSON"];
if (fh == nil){
NSLog(@"Cant open");
return -1;
}
//
// Read it in
//
NSData *JSONText;
NSLog(@"START READING");
JSONText = [fh readDataToEndOfFile];
NSLog(@"DONE READING");
//
// Load it into NSDictionary
//
NSError *JSONerror;
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONText options:NSJSONReadingMutableContainers error:&JSONerror];
NSLog(@"Name: %@",[JSONDictionary objectForKey:@"CrosswalkName"]);
gemList = [JSONDictionary objectForKey:@"GEM"] ;
//
// Set up Core Data
//
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UniversalGEM" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"codesetID" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSManagedObject *newManagedObject;
//
// Loop through and update database
//
int recordsWritten = 0 ;
for (NSDictionary *gem in gemList) {
newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSLog(@"Source Code: %@",[gem objectForKey:@"SourceCode"]);
[newManagedObject setValue:[gem objectForKey:@"ApproximateFlag"] forKey:@"approximateFlag"];
[newManagedObject setValue:[gem objectForKey:@"ChoiceList"] forKey:@"choiceList"];
[newManagedObject setValue:[gem objectForKey:@"CombinationFlag"] forKey:@"combinationFlag"];
[newManagedObject setValue:[JSONDictionary objectForKey:@"CrosswalkName"] forKey:@"crosswalkName"];
[newManagedObject setValue:[gem objectForKey:@"NoMapFlag"] forKey:@"noMapFlag"];
[newManagedObject setValue:[gem objectForKey:@"Scenario"] forKey:@"scenario"];
[newManagedObject setValue:[gem objectForKey:@"SourceCode"] forKey:@"sourceCode" ];
[newManagedObject setValue:[gem objectForKey:@"SourceDescription"] forKey:@"sourceDescription"];
[newManagedObject setValue:[gem objectForKey:@"TargetCode"] forKey:@"targetCode" ];
[newManagedObject setValue:[gem objectForKey:@"TargetDescription"] forKey:@"targetDescription"];
// Save the managed object context
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
recordsWritten++;
}
NSLog(@"Records written: %d", recordsWritten);
}