objective c - NSURLSession uploadTaskWithRequest - use block within completion handler -


i have class manages calls api. has method manage this, lets call callapimethod:

this method accepts success , fail block.

inside method, call uploadtaskwithrequest make call api. within uploadtaskwithrequest completion handler i'd (depending on result) pass results through either success or fail blocks.

i'm having issues this. works , keeping super tidy when call callapimethod using success/fail blocks it's locking ui/mainthread rather being asynchronous i'd expect.

how should go implementing pattern? or there better way go it?

i don't need support pre-ios7.

thanks

edit: basic implementation discussed above.

- (void)callapimethod:(nsstring *)method withdata:(nsstring *)requestdata as:(krequesttype)requesttype success:(void (^)(id responsedata))success failure:(void (^)(nsstring *errordescription))failure {     [redacted]     nsurlsession *session = [nsurlsession sharedsession];     nsurlsessiondatatask *task = [session uploadtaskwithrequest:request                                                        fromdata:postdata                                               completionhandler:                                   ^(nsdata *data, nsurlresponse *response, nserror *error) {                                       if (error) {                                           failure(error.description);                                       } else {                                           nserror *jsonerror;                                           id responsedata = [nsjsonserialization                                                              jsonobjectwithdata:data                                                              options:kniloptions                                                              error:&jsonerror];                                           if (jsonerror) {                                               failure(jsonerror.description);                                           } else {                                               success(responsedata);                                           }                                       }                                   }];      [task resume]; } 

callapi method, used follows (from uitableviewcontroller):

[apicontroller callapimethod:@"users.json?action=token"                     withdata:logindata                           as:krequestpost                      success:^(id responsedata) {                          if ([responsedata iskindofclass:[nsdictionary class]]) {                              if ([responsedata objectforkey:@"token"]) {                                  //store token/credentials                              } else if ([responsedata objectforkey:@"error"]) {                                  //error                                  [self displayerrormessage:[responsedata objectforkey:@"error"]];                                  return;                              } else {                                  //undefined error                                  [self displayerrormessage:nil];                                  return;                              }                          } else {                              //error                              [self displayerrormessage:nil];                              return;                          }                           //if login success                       }                      failure:^(nsstring *errordescription) {                          [self displayerrormessage:errordescription];                      }]; 

your nsurlsession code looks fine. i'd suggest adding breakpoints can identify if deadlocking somewhere , if so, where. nothing in code sample suggest such problem.

i suggest ensure ui calls dispatched main queue. nsurlsessionuploadtask completion handler may called on background queue, ui updates (alerts, navigation, updating of uiview controls, etc.) must take place on main queue.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -