从 URL 向 NSMutableArray 添加对象的更快方法
Faster way to add object to NSMutableArray from URL
I am Working on an app where user have to select hometown and country
for signup. For this, I' m getting country list and cities list from
URL
, adding in NSMutableArray
and populating in UIPickerView
. Now, the
issue is When I call method for getting country list,it takes 5-6
seconds to load.Then,I have to call the method for getting cities list
corresponding to country name. But, the count of cities is more.So, it
takes long time to add cities name in array. Here is my code to get
cities list.
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];
}
}];
So, if there's any faster way to add object in NSMutableArray
and
Populate UIPickerView
. Any help would be appreciated. Thanks.
正如我在上面的评论中所提到的,如果列表足够大,NSLog
将是一个重要的时间使用。将其从循环中删除会大大加快速度。
I am Working on an app where user have to select hometown and country for signup. For this, I' m getting country list and cities list from
URL
, adding inNSMutableArray
and populating inUIPickerView
. Now, the issue is When I call method for getting country list,it takes 5-6 seconds to load.Then,I have to call the method for getting cities list corresponding to country name. But, the count of cities is more.So, it takes long time to add cities name in array. Here is my code to get cities list.
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];
}
}];
So, if there's any faster way to add object in
NSMutableArray
and PopulateUIPickerView
. Any help would be appreciated. Thanks.
正如我在上面的评论中所提到的,如果列表足够大,NSLog
将是一个重要的时间使用。将其从循环中删除会大大加快速度。