Thursday 15 January 2015

objective c - Slow tableView reload with Yelp API iOS -



objective c - Slow tableView reload with Yelp API iOS -

i playing around yelp's api. have managed create little app allows user input 'term' , 'location', , tableview containing 5 businesses appear. however, takes 30 seconds min info appear on tableview after clicking search. if log out jsonresponse, info in nsarrray in second, still doesn't appear in tableview. sometimes, businesses won't appear @ all.

the code utilize search.

- (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); // avoids programme exiting before our asynchronous callbacks have been made. }

you have reload table info using performselector on main thread can got info on time remove dispatch grouping code e.g

-

(void)querytopbusinessinfoforterm:(nsstring *)term location:(nsstring *)location category:(nsstring *)category completionhandler:(void (^)(nsdictionary *topbusinessjson, nserror *error))completionhandler { nslog(@"querying search api term \'%@\' , location \'%@' , 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 found } } else { completionhandler(nil, error); // error happened or http response not 200 ok } }] resume];

ios objective-c uitableview

No comments:

Post a Comment