ios - completion handler in method which request information from server -
i have method send request server , have created call function completionhandler follows:
-(void)sendaddsubscriptionrequest:(nsstring*)owner withcomppletionhandler:(void (^)(bool, nsarray *, nserror *))completion datawebservice = [[nsmutabledata alloc] init]; nsurl* aurl = [nsurl urlwithstring:[nsstring stringwithformat:@"https://www.google.com/add?"]]; nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:aurl cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:30.0]; [request addvalue:[nsstring stringwithformat:@"bearer %@", "token"] forhttpheaderfield:@"authorization"]; [request sethttpmethod:@"post"]; nsstring* postdata = [[nsstring alloc] initwithformat:@"owner=%@", owner]; [request sethttpbody:[postdata datausingencoding:nsutf8stringencoding]]; nsdata* returndata = [nsurlconnection sendasynchronousrequest:request queue:nil completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { }; my problem how should return completion handler function after data fetched.
thanks,.
as sending asynchronous request, method return data "returned" in nsurlconnection completion handler. can decode there , pass along own completion handler:
[nsurlconnection sendasynchronousrequest:request queue:nil completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { nsarray *array = nil; if (!connectionerror) array = /* data somehow */ completion(!connectionerror, array, connectionerror); }]; note: it's not clear completion handler parameters mean, might have taken liberties.
Comments
Post a Comment