将100万条记录保存到核心数据需要花费很多时间

时间:2014-04-07 11:13:29

标签: ios core-data objective-c-blocks

我将CSV中的100万条记录保存到核心数据库中。保存所有记录大约需要35分钟。无论如何都要减少插入时间。这是我用来插入记录的代码:

 NSString *resourceFileName = @"npidata_20140209_reduced2";
NSString *pathToFile =[[NSBundle mainBundle] pathForResource: resourceFileName ofType: @"csv"];

NSString* content = [NSString stringWithContentsOfFile:pathToFile
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
__block int count = 0;

[content enumerateLinesUsingBlock:^(NSString * line,BOOL * stop){         的NSLog(@" LINE ==%@",线);

    NSArray *lineComponents=[line componentsSeparatedByString:@","];
    if(lineComponents){       
            float f=[[lineComponents objectAtIndex:0] floatValue];
            NSString *strNPI=[lineComponents objectAtIndex:0];
            NSString *strProviderLastName=[lineComponents objectAtIndex:1];
            NSString *strProviderFirstName=[lineComponents objectAtIndex:2];
            NSString *strLicenseNumber=[lineComponents objectAtIndex:6];
            NSString *strLicenseState=[lineComponents objectAtIndex:7];         
            NSManagedObject *object=[NSEntityDescription insertNewObjectForEntityForName:@"AttendeeInformation" inManagedObjectContext:[DataBaseManager sharedInstance].managedObjectContext];

            NSString *strFullName = [NSString stringWithFormat:@"%@ %@",strProviderFirstName,strProviderLastName];

            [object setValue:strNPI forKey:@"npiNumber"];
            [object setValue:strProviderFirstName forKey:@"attendeeFirstName"];
            [object setValue:strProviderLastName forKey:@"attendeeLastName"];
            [object setValue:strFullName forKey:@"attendeeName"];
            [object setValue:strLicenseNumber forKey:@"licnesedNumber"];
            [object setValue:strLicenseState forKey:@"stateLicense"];

            NSError *error;
            count++;
            if(count%100==0)
            {
                if (![[DataBaseManager sharedInstance].managedObjectContext save:&error])
                {
                    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                }
                [[DataBaseManager sharedInstance].managedObjectContext refreshObject:object mergeChanges:YES];
            }
        if (count==lineComponents.count)
        {
            completionHandler();
        }
    }
}];

2 个答案:

答案 0 :(得分:3)

您应该使用时间配置文件和核心数据运行仪器,以查看您的瓶颈是什么。这是验证性能问题的最佳方式。核心数据可能是您的问题,也可能不是。

答案 1 :(得分:2)

@Desdenova没错。您需要获取上下文中的所有记录,并在结束时保存上下文,或者至少在非常大的批次中保存。在API下,sqlite为每个上下文保存执行单独的事务,这非常昂贵。如果您将所有操作作为单个事务执行,则速度要快几个数量级。在您评论的地方进行测试:

            if (![[DataBaseManager sharedInstance].managedObjectContext save:&error])
            {
                NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            }

并在循环后添加相同的3行。