Using objective-c blocks recursively with iOS Twitter API -
so i'm trying use built in twitter api in ios 5 retrieve list of followers given user. in example documentation can find, requests made api passing inline blocks executed when request returns, fine of simpler stuff, when i'm trying ~1000 followers, , request returning them paged in sizes ~100, i'm stuck on how recursively call request again using 'next paging address' returned , processed inside completion block. here code:
- (void)gettwitterfollowers { // first, need obtain account instance user's twitter account acaccountstore *store = [[acaccountstore alloc] init]; acaccounttype *twitteraccounttype = [store accounttypewithaccounttypeidentifier:acaccounttypeidentifiertwitter]; // request access user access twitter accounts [store requestaccesstoaccountswithtype:twitteraccounttype withcompletionhandler:^(bool granted, nserror *error) { if (!granted) { // user rejected request nslog(@"user rejected access account."); } else { // grab available accounts nsarray *twitteraccounts = [store accountswithaccounttype:twitteraccounttype]; if ([twitteraccounts count] > 0) { // use first account simplicity acaccount *account = [twitteraccounts objectatindex:0]; // make authenticated request our endpoint nsmutabledictionary *params = [[nsmutabledictionary alloc] init]; [params setobject:@"1" forkey:@"include_entities"]; // endpoint wish call nsurl *url = [nsurl urlwithstring:@"http://api.twitter.com/1/followers.json"]; // build request our parameter request = [[twrequest alloc] initwithurl:url parameters:params requestmethod:twrequestmethodget]; [params release]; // attach account object request [request setaccount:account]; [request performrequestwithhandler:^(nsdata *responsedata, nshttpurlresponse *urlresponse, nserror *error) { if (!responsedata) { // inspect contents of error fulllog(@"%@", error); } else { nserror *jsonerror; followers = [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:&jsonerror]; if (followers != nil) { // data returned here contains next page value needed request next 100 followers, //what best way use this?? fulllog(@"%@", followers); } else { // inspect contents of jsonerror fulllog(@"%@", jsonerror); } } }]; } // if ([twitteraccounts count] > 0) } // if (granted) }]; [store release]; }
ideally i'd way listen data being returned, check next page value , if exists, reuse code block , append data returned. i', sure there must 'best-practice' way achieve this, appreciated!
to expand on @eimantas' answer, request handler expecting specific block signature, need different way handle page number.
-(void)gettwitterfollowers { // set request... __block int page = 0; __block void (^requesthandler)(nsdata*, nshttpurlresponse*, nserror*) = null; __block twrequest* request = [[twrequest alloc] initwithurl:url parameters:params requestmethod:twrequestmethodget]; requesthandler = [^(nsdata *responsedata, nshttpurlresponse *urlresponse, nserror *error) { followers = [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:&jsonerror]; if (followers != nil) { // process followers page++; nsmutabledictionary *params = [nsmutabledictionary dictionarywithdictionary:request.parameters]; // update params page number request = [[twrequest alloc] initwithurl:url parameters:params requestmethod:twrequestmethodget]; [request performrequestwithhandler:requesthandler]; } } copy]; // call block first page [request performrequestwithhandler:requesthandler]; }
Comments
Post a Comment