【发布时间】:2015-01-10 08:20:12
【问题描述】:
我正在开发一个用户必须选择家乡和国家/地区的应用 注册。为此,我得到了国家列表和城市列表
URL,添加NSMutableArray并填充UIPickerView。现在 问题是当我调用获取国家列表的方法时,需要 5-6 秒加载。然后,我必须调用获取城市列表的方法 对应国名。但是,城市的数量更多。所以,它 在数组中添加城市名称需要很长时间。这是我要获取的代码 城市列表。
NSString *urlString=[NSString stringWithFormat:@"%@/selected_city",BaseURL];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *postData=[NSString stringWithFormat:@"country_name=%@",self.countryField.text];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"posted data is %@",postData);
[request setValue:@"binary"
forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8"
forHTTPHeaderField:@"Content-Type"];
[self loadCountryList];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if(data)
{
NSError *error = [[NSError alloc] init];
NSDictionary* responseDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseStr);
if ([[responseDict valueForKey:@"message"] isEqualToString:@"Request Successfull"])
{
NSArray *predictArr = [responseDict objectForKey:@"city_list"];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
/* Fetch the image from the server... */
dispatch_async(dispatch_get_main_queue(), ^{
for(int i=0;i<[predictArr count];i++)
{
NSMutableDictionary *data=[[predictArr objectAtIndex:i] mutableCopy];
[cityArray addObject:data];
NSLog(@"countries array is %@",cityArray);
}
[self stopLoading];
[cityPicker reloadAllComponents];
});
});
}
else
{
[self stopLoading];
}
}
else
{
[self stopLoading];
}
}];
所以,如果有任何更快的方法可以在
NSMutableArray中添加对象并且 填充UIPickerView。任何帮助,将不胜感激。谢谢。
【问题讨论】:
-
您是否(使用 Instruments)对其进行了分析以查看真正减慢速度的部分?网络连接或代码?另外,如果你在谈论一个非常大的数组,
NSLog会减慢你的速度 -
@jn_pdx,谢谢。成功了,不是连接问题。删除 NSLog 使其更快。
-
@Sushil 如果您想在不影响性能的情况下进行日志记录,请尝试使用 CocoaLumberjack 进行异步日志记录。
标签: objective-c ios8 nsmutablearray uipickerview nsmutabledictionary