ios - understanding blocks (login with twitter) -
i'm trying understand blocks in objective-c. take example logging twitter. want main thread wait until block finished.
how can achieve in following code. method showmain transitioning next view. array tweetarray empty because async block not finished. right now, i'm using method performselectoronmainthread:withobject:waituntildone:. please explain concept of blocks in detail.
[account requestaccesstoaccountswithtype:accounttype options:nil completion:^(bool granted, nserror *error) { [mbprogresshud showhudaddedto:self.view animated:yes]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_low, 0), ^{ if (granted) { nsarray *accounts = [account accountswithaccounttype:accounttype]; // check if users has setup @ least 1 twitter account if (accounts.count > 0) { acaccount *twitteraccount = [accounts objectatindex:0]; slrequest *twitterinforequest = [slrequest requestforservicetype:slservicetypetwitter requestmethod:slrequestmethodget url:[nsurl urlwithstring:@"https://api.twitter.com/1.1/users/show.json"] parameters:[nsdictionary dictionarywithobject:twitteraccount.username forkey:@"screen_name"]]; [twitterinforequest setaccount:twitteraccount]; // making request [twitterinforequest performrequestwithhandler:^(nsdata *responsedata, nshttpurlresponse *urlresponse, nserror *error) { dispatch_async(dispatch_get_main_queue(), ^{ if ([urlresponse statuscode] == 429) { return; } if (error) { return; } // check if there response data if (responsedata) { nserror *error = nil; nsarray *tweetarray = [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:&error]; nslog(@"%@", tweetarray); } }); }]; } } else { nslog(@"access denied"); } }); dispatch_async(dispatch_get_main_queue(), ^{ [mbprogresshud hidehudforview:self.view animated:yes]; [self performselectoronmainthread:@selector(showmain) withobject:nil waituntildone:yes]; }); }];
thanks, mike
explain whole concept of blocks in answer ask much, should read documentation , search in books.
but looking directly problem, try tell what's going on:
this method performrequestwithhandler:
twitterinforequest
object, name says, performs requisition twitter's servers, returning responsedata
. method receives parameter block, called when requisition arrives. so, block nothing piece of code executed handle
responsedata
, when receive it.
so, if want after response arrives, must put inside block. in case, call showmain
method:
(...) [twitterinforequest performrequestwithhandler:^(nsdata *responsedata, nshttpurlresponse *urlresponse, nserror *error) { dispatch_async(dispatch_get_main_queue(), ^{ if ([urlresponse statuscode] == 429) { return; } if (error) { return; } // check if there response data if (responsedata) { nserror *error = nil; nsarray *tweetarray = [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:&error]; nslog(@"%@", tweetarray); [self showmain]; } }); }];
ps: executing handle block inside main thread (with dispatch_async(dispatch_get_main_queue()...
) don't have call method through performselectoronmainthread
.
Comments
Post a Comment