objective c - How is it better to wait an asynchronous method to be finished in iPhone app? -
everybody. want understand, how shoud procceed situations when asynchronous method has "didfinish:@selector(sel)" parameter. code example is:
// // authentication check - ( void )authenticationsuccess: ( gdataserviceticket* ) ticket authenticatedwitherror: ( nserror* ) error { if ( error == nil ) { nslog( @"authentication success" ); } else { nslog( @"authentication error" ); } } // - ( void ) fetchfeedofspreadsheets { //create , authenticate google spreadsheet service if ( !(mservice) ) { gdataservicegooglespreadsheet *service = [self spreadsheetservice]; [mservice autorelease]; mservice = [service retain]; } // check autentication success ( invoke "authenticationsuccess" method debug success & error ) [mservice authenticatewithdelegate: self didauthenticateselector:@selector(authenticationsuccess: authenticatedwitherror:) ]; // here want make pause , whait result, either authenticated or not // , make "if" statement contintue working on server, or return error //fetch retrieves feed of spreadsheets entries nsurl *feedurl = [ nsurl urlwithstring: kgdatagooglespreadsheetsprivatefullfeed ]; gdataserviceticket *ticket; ticket = [mservice fetchfeedwithurl: feedurl delegate: self didfinishselector: @selector(spreadsheetsticket:finishedwithfeed: error: ) ]; // here want wait second time. want "spreadsheetsticket: // finishedwithfeed:error:" procceed error , put feed in nsarray object // , after want work nsarray right here }
i's clear, can push code want end of "authenticationsuccess" method section, it's clear, it's wrong way solve proble. there number of situations this, call asynchronous method selector parameter, , want find solution providing me flexible code writing.
thanks in advance.
it's standard practice in objective-c put code executed after authentication in authenticationsucess:
method. might not it, life.
many people had same complaint you, on ios 4 , later, there's called blocks allow write code executed after authentication in method initiates authentication, in
[mservice authenticateandexecute:^{ code executed when authenticated ; } whenerror:^{ code executed when authentication failed; } ];
but in case need modify api, possible using categories. see this blog post mike ash. has many other posts on blocks on same blog, instructive.
Comments
Post a Comment