使用OSX加载SQLite不会写入所有数据

时间:2015-04-15 00:31:44

标签: json macos sqlite

我有一个OSX应用程序,它读取JSON文件并使用Core Data将行插入SQLite数据库。我的问题是,即使应用程序报告所有写入的行,也不会加载JSON文件中的每一行。

它始终是最后几百行左右的未写入,这使我相信我没有关闭/刷新最后一次写入数据库。

我在示例或本网站上看不到此功能。 这是代码:

 @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);
    }

0 个答案:

没有答案