更快地从URL向NSMutableArray添加对象的方法

时间:2015-01-10 08:20:12

标签: objective-c ios8 nsmutablearray uipickerview nsmutabledictionary

  

我正在开发一个用户必须选择家乡和国家的应用程序   注册。为此,我'从中获取国家/地区列表和城市列表   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];
         }
     }];
  

所以,如果有更快的方法在NSMutableArrayUIPickerView中添加对象   填充{{1}}。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

正如我在上面的评论中所提到的,NSLog将充分利用时间给出一个足够大的列表。从循环中删除它会大大加快速度。