0

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);
    }
  • Are you using Write Ahead logging do you know? – JamesSugrue Apr 15 '15 at 00:40
  • Yes, I believe I am. While the load is occurring I see a .sqlite-wal file. I'll check again, but I don't remember seeing a way to turn that off in Core Data. – Sean Murphy Apr 17 '15 at 02:08
  • I had a similar issue that you're having, and did manage to disable it. Sorry to say I can't remember how, and now can't find it again either. There was a flag tho. – JamesSugrue Apr 17 '15 at 02:26
  • Maybe? http://stackoverflow.com/questions/20062197/how-to-disable-wal-journal-mode – JamesSugrue Apr 17 '15 at 02:27

0 Answers0