ios - Calling two NSURLSessionTask completion blocks one after another? -


i have following setup uses afnetworking make calls server. have used example found on internet include completion block know when call has finished.

file "fcengine.m"

- (void)fetchbusinessprofile:(nsstring *)userid useraccesstoken:(nsstring *)useraccesstoken completion:(void (^)(nsdictionary *json, bool success))completion {  /// validate user token again user id.  nsdictionary *parameters = [[nsdictionary alloc]initwithobjectsandkeys:useraccesstoken,@"user_access_token",                             userid,@"user_id",                             nil];   afhttpsessionmanager *manager = [afhttpsessionmanager manager];  afhttprequestserializer *serializer = [afhttprequestserializer serializer]; manager.requestserializer = serializer;  manager.responseserializer = [afjsonresponseserializer serializerwithreadingoptions:nsjsonreadingallowfragments];  [manager post:@"" parameters:parameters progress:nil success:^(nsurlsessiontask *task, id responseobject) {      nslog(@"json business profile: %@", responseobject);      fetchbusinessprofilecompletion(responseobject, yes);  } failure:^(nsurlsessiontask *operation, nserror *error) {      //nslog(@"error: %@", error);      nsmutabledictionary *errorresponse = [[nsmutabledictionary alloc] init];      [errorresponse setobject:@"connection_error" forkey:@"state"];     [errorresponse setobject:[error localizeddescription] forkey:@"description"];      fetchbusinessprofilecompletion(errorresponse, yes);  }];  }  - (void)fetchnotifications:(nsstring *)userid useraccesstoken:(nsstring *)useraccesstoken completion:(void (^)(nsdictionary *json, bool success))completion {  /// validate user token again user id.  nsdictionary *parameters = [[nsdictionary alloc]initwithobjectsandkeys:useraccesstoken,@"user_access_token",                             userid,@"user_id",                             nil];   afhttpsessionmanager *manager = [afhttpsessionmanager manager];  afhttprequestserializer *serializer = [afhttprequestserializer serializer]; manager.requestserializer = serializer;  manager.responseserializer = [afjsonresponseserializer serializerwithreadingoptions:nsjsonreadingallowfragments];  [manager post:@"" parameters:parameters progress:nil success:^(nsurlsessiontask *task, id responseobject) {      //nslog(@"json: %@", responseobject);      completion(responseobject, yes);  } failure:^(nsurlsessiontask *operation, nserror *error) {      //nslog(@"error: %@", error);      nsmutabledictionary *errorresponse = [[nsmutabledictionary alloc] init];      [errorresponse setobject:@"connection_error" forkey:@"state"];     [errorresponse setobject:[error localizeddescription] forkey:@"description"];      completion(errorresponse, yes);  }];   } 

the following how make call on main view controller

- (void)mymethods {   [self.fcengine fetchbusinessprofile:userid useraccesstoken:useraccesstoken completion:^(nsdictionary *json, bool success) {      /// response here   }];  [self.fcengine fetchnotifications:self.userid useraccesstoken:self.useraccesstoken completion:^(nsdictionary *json, bool success) {         //// response here   }];  } 

now problem 2 calls made 1 after , when fetch data 1 e.g. "fetchbusinessprofile" competition block on both called.

have set wrong? if 2 or more calls want completion called particular block , not them all.

i don't think understand asynchronous completion blocks. if make 2 network calls defined above, can happen in order. completion in fetchbusinessprofile , fetchnotifications different completion blocks ... unless make them same.

for example:

 [self.fcengine fetchbusinessprofile:userid useraccesstoken:useraccesstoken completion:^(nsdictionary *json, bool success) {      /// handle response     // note calling same completion block     samecompletionblockalreadydefined();   }];  [self.fcengine fetchnotifications:self.userid useraccesstoken:self.useraccesstoken completion:^(nsdictionary *json, bool success) {      //// handle response     // note calling same completion block     samecompletionblockalreadydefined();   }]; 

in case, samecompletionblockalreadydefined() defined block. in case, body of block of each call indeed, funnel same call via samecompletionblockalreadydefined. possible confused because completion happens named same in first snippet.

note question poorly phrased isn't clear on mean.

the larger question goal? want 1 completion block called @ end? or want distinct completion blocks? both require different techniques. clear on goal is.

the former best service dispatch_group. latter requires different completion blocks.

an example of dispatch group like:

dispatch_group_t group = dispatch_group_create();  dispatch_group_enter(group);  [self.fcengine fetchbusinessprofile:userid useraccesstoken:useraccesstoken completion:^(nsdictionary *json, bool success) {      /// handle response     dispatch_group_leave(group);   ];  self.fcengine fetchnotifications:self.userid useraccesstoken:self.useraccesstoken completion:^(nsdictionary *json, bool success) {      //// handle response     dispatch_group_leave(group);  }];  dispatch_group_wait(group, dispatch_time_forever);  // completion block means done completion(); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -