ios - Setting UILabel text taking longer than expected iOS7 -
in view attempt display weather related info. use
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ make nsurl , other related things send nsurlrequest set 2 uilabels data nslog(show data retrieved) });
for reason, see nslog line 15-45 seconds before uilabel changes new text. new obj-c , lot of code comes using tutorial dont have greatest understanding of dispatch_async method. thoughts or suggestions appreciated.
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nsurlresponse *response = nil; nserror *error = nil; nsdata *respdata = nil; nsurlrequest *forecastrequest = [nsurlrequest requestwithurl:currentforecasturl]; respdata = [nsurlconnection sendsynchronousrequest:forecastrequest returningresponse:&response error:&error]; [_responsedata appenddata:respdata]; nslog(@"response: %@", response); nslog(@"error: %@", error); id json = [_responsedata yajl_json]; currentforecast = [[nsarray alloc]initwithobjects:[json objectforkey:@"temperature"],[json objectforkey:@"feelslike"],nil]; [_temperaturelabel settext:[nsstring stringwithformat:@"%@",[json objectforkey:@"temperature"]]]; [_tempdescriptionlabel settext:[nsstring stringwithformat:@"%@",[json objectforkey:@"desc"]]]; [uiview beginanimations:nil context:null]; [uiview setanimationduration:2.0]; [_tempdescriptionlabel setalpha:1]; [_temperaturelabel setalpha:1]; [uiview commitanimations]; nslog(@"done"); nslog(@"json: %@", [json yajl_jsonstringwithoptions:yajlgenoptionsbeautify indentstring:@" "]); });
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)
is used create background thread, user interface should updated in main thread
like this:
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ //do stuff in background dispatch_async(dispatch_get_main_queue(), ^{ // update ui }); });
Comments
Post a Comment