我正在使用Dave Delong的CHCSV解析器将数据从CSV文件加载到iOS应用程序中。该应用程序是我正在更新工作的应用程序。 我发现应用程序似乎需要很长时间才能将所有数据加载到应用程序中。只有超过13000行数据,加载大约需要10分钟。
我没有很多CSV解析或Objective-C的经验,但我一直试图让这个应用程序更快地运行。当我第一次开始使用它时,解析器是Dave放在GitHub上的原始解析器,但我已将其更新为最近的提交,但此更改仅将加载时间从15分钟减少到10分钟。
正在收集所有数据,我对此没有任何问题,我只是想知道为什么我在加载时遇到这样的问题。我没有对Dave的代码进行任何更改。
如果有人遇到这个问题或者知道可以解决的问题,我会非常感激。我花了很长时间在网上寻求帮助,但似乎没有人在加载时遇到问题。我不确定我的任何代码是否会导致问题,所以如果需要任何代码片段来帮助我,我非常愿意给他们。
以下是带有JSON的代码:
NSString *url = [_configDictionary objectForKey:@"stationCSVURL"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[url release];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[request release];
NSError *jsonError = nil;
NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
[data release];
[self clearDatabase];
int count = 0;
for (NSObject* stations in tempDict) {
NSLog(@"%i", count);
_station = [[StationInfo alloc] init];
NSString *tempID =[tempDict valueForKeyPath:@"ID"][count];
if(tempID && (tempID != [NSNull null])){
_station.iD = [[tempDict valueForKeyPath:@"ID"][count] intValue];
}
else _station.iD = 0;
NSString *tempName = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Title"];
if(tempName && (tempName != [NSNull null]) && [tempName isKindOfClass:[NSString class]]){
_station.name = tempName;
}
else _station.name = @"N/A";
NSString *tempAddress = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"AddressLine1"];
if(tempAddress && (tempAddress != [NSNull null]) && [tempAddress isKindOfClass:[NSString class]]){
_station.address = tempAddress;
}
else _station.address = @"N/A";
NSString *tempCity = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Town"];
if(tempCity && (tempCity != [NSNull null]) && [tempCity isKindOfClass:[NSString class]]){
_station.city = tempCity;
}
else _station.city = @"N/A";
NSString *tempState = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"StateOrProvince"];
if(tempState && (tempState != [NSNull null]) && [tempState isKindOfClass:[NSString class]]){
_station.state = tempState;
}
else _station.state = @"N/A";
NSString *tempZip = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Postcode"];
if(tempZip && (tempZip != [NSNull null]) && [tempZip isKindOfClass:[NSString class]]){
_station.zip = [[[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Postcode"] intValue];
}
else _station.zip = 0;
NSString *tempPhone = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"ContactTelephone1"];
if(tempPhone && ![tempPhone isKindOfClass:[NSNull class]]){
_station.phone = tempPhone;
}
else _station.phone = @"N/A";
NSString *tempComments = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"AccessComments"];
if(tempComments && ![tempComments isKindOfClass:[NSNull class]]){
_station.daysTime = tempComments;
}
else _station.daysTime = @"N/A";
NSString *tempLat = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Latitude"];
if(tempLat){
_station.latitude = [[[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Latitude"] floatValue];
}
else _station.latitude = 0;
NSString *tempLong = [[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Longitude"];
if(tempLong){
_station.longitude = [[[tempDict valueForKeyPath:@"AddressInfo"][count] objectForKey:@"Longitude"] floatValue];
}
else _station.longitude = 0;
if([tempDict valueForKey:@"UsageType"][count] != [NSNull null]) {
NSString *tempUsage = [[tempDict valueForKeyPath:@"UsageType"][count] objectForKey:@"Title"];
if(tempUsage){
_station.publicOrPrivate = tempUsage;
}
else _station.publicOrPrivate = @"N/A";
}
NSDictionary *tempDict2 = [tempDict valueForKeyPath:@"Connections"][count];
NSString *tempConnect = [[tempDict2 valueForKeyPath:@"ConnectionType"][0] objectForKey:@"Title"];
if(tempConnect){
_station.connectionType = tempConnect;
}
else _station.connectionType = @"N/A";
count++;
if(count % 100 == 0) NSLog(@"%i stations loaded.", count);
if (_station.name) [self addStationToDatabase:_station];
[_station release];
}