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

Json 用OSX加载SQLite不会写入所有数据,json,macos,sqlite,Json,Macos,Sqlite,我有一个OSX应用程序,它读取JSON文件并使用核心数据将行插入SQLite数据库。我的问题是,即使应用程序报告所有写入的行,JSON文件中并不是每一行都被加载 总是最后几百行左右没有写入,这让我相信我没有关闭/刷新对数据库的最后写入 我在示例或本网站上没有看到此函数。 代码如下: @autoreleasepool { NSManagedObjectContext *context = managedObjectContext(); NSArray *gemLi

我有一个OSX应用程序,它读取JSON文件并使用核心数据将行插入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);
    }

你在使用写前日志记录吗?知道,我想是的。在加载过程中,我看到一个.sqlite文件。我会再次检查,但我不记得在核心数据中看到了关闭该功能的方法。我遇到了与您类似的问题,并且成功地禁用了它。很抱歉,我不记得怎么找了,现在也找不到了。有一面旗子。也许吧?