簡體   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