繁体   English   中英

用Yelp API iOS重新加载慢的tableView

[英]Slow tableView reload with Yelp API iOS

我在玩Yelp的API。 我设法创建了一个小应用程序,允许用户输入'term'和'location',然后出现一个包含5个业务的tableView。 但是,单击搜索后,数据大约需要30秒到一分钟才能显示在表格视图上。 如果我注销JSONresponse,数据将在一秒钟内存储在我的NSArrray中,但仍未出现在tableView中。 有时,这些业务根本不会出现。

我用来搜索的代码。

    - (IBAction)searchButtonPressed:(UIButton *)sender
{
    YPAPISample *YPAPI = [[YPAPISample alloc]init];

    NSString *term = self.termTextField.text;
    NSString *location = self.locationTextField.text;

    dispatch_group_t requestGroup = dispatch_group_create();

    dispatch_group_enter(requestGroup);

    [YPAPI queryBusinessesForTerm:term location:location completionHandler:^(NSArray *jsonResponse, NSError *error) {
        if (!error) {

            self.businessInfoArray = jsonResponse;
            NSLog(@"%lu",[businessInfoArray count]);
            [self.tableView reloadData];
        }
        else NSLog(@"%@", error);

        dispatch_group_leave(requestGroup);
    }];


        dispatch_group_wait(requestGroup, DISPATCH_TIME_FOREVER); // This avoids the program exiting before all our asynchronous callbacks have been made.


}

您必须在主线程上使用performselector重新加载表数据,您可以轻松地准时获取数据,从代码中删除此调度组,例如

--

 (void)queryTopBusinessInfoForTerm:(NSString *)term location:(NSString *)location category:(NSString *)category completionHandler:(void (^)(NSDictionary *topBusinessJSON, NSError *error))completionHandler
{

    NSLog(@"Querying the Search API with term \'%@\' and location \'%@' and category '%@'", term, location,category);


    NSURLRequest *searchRequest = [self _searchRequestWithTerm:term location:location category:category];
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithRequest:searchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

        if (!error && httpResponse.statusCode == 200)
        {

            NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            NSArray *businessArray = searchResponseJSON[@"businesses"];

            if ([businessArray count] > 0)
            {

                NSLog(@"total businesses==>%@",[searchResponseJSON valueForKey:@"total"]);

                lblresults.text = [NSString stringWithFormat:@"%@",[searchResponseJSON valueForKey:@"total"]];

                 hotelListArray = [businessArray mutableCopy];
                allObjectArray = [businessArray mutableCopy];
                 self.tableHotels.hidden = NO;

                [self performSelectorOnMainThread:@selector(ReloadTableData:) withObject:hotelListArray waitUntilDone:YES];
            }
            else
            {
                completionHandler(nil, error); // No business was found

            }
        }
        else
        {
            completionHandler(nil, error); // An error happened or the HTTP response is not a 200 OK

        }
    }] resume]; 

暂无
暂无

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

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