繁体   English   中英

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

[英]Faster way to add object to NSMutableArray from URL

我正在开发一个应用程序,用户必须选择家乡和国家/地区进行注册。 为此,我要从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 任何帮助,将不胜感激。 谢谢。

正如我在上面的评论中所提到的,给定足够大的列表, NSLog将大量使用时间。 从循环中删除它可以大大加快速度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM