gpt4 book ai didi

ios - 将 100 万条记录保存到核心数据需要花费大量时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:28:21 24 4
gpt4 key购买 nike

我正在将 100 万条记录从 CSV 保存到核心数据库。保存所有记录大约需要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;

[内容 enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { NSLog(@"LINE==%@",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();
}
}
}];

最佳答案

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

关于ios - 将 100 万条记录保存到核心数据需要花费大量时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com