Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 停止方法中的执行以调用另一个方法,然后继续_Objective C - Fatal编程技术网

Objective c 停止方法中的执行以调用另一个方法,然后继续

Objective c 停止方法中的执行以调用另一个方法,然后继续,objective-c,Objective C,在objective-c中,处理这种情况的最佳方法是什么。在我对远程API的所有调用中,我需要首先确保我有令牌。如果可能的话,我宁愿不在每次通话前检查令牌 DO NOT WANT TO DO THIS FOR EVERY API CALL! #if (token) { makeGetForTweetsRequestThatRequiresToken } 如果我需要一个令牌,可能它已过期,那么该调用可能需要一些时间才能返回,因此我需要等待它返回,然后再进行下一个API调用。是否可以执行

在objective-c中,处理这种情况的最佳方法是什么。在我对远程API的所有调用中,我需要首先确保我有令牌。如果可能的话,我宁愿不在每次通话前检查令牌

DO NOT WANT TO DO THIS FOR EVERY API CALL!
#if (token) { 
   makeGetForTweetsRequestThatRequiresToken
 }
如果我需要一个令牌,可能它已过期,那么该调用可能需要一些时间才能返回,因此我需要等待它返回,然后再进行下一个API调用。是否可以执行以下操作

[thing makeGetForTweetsRequestThatRequiresToken];


-(void)makeGetForTweetsRequestThatRequiresToken {
      if(nil == token) {

         // make another API call to get a token and save it
         // stop execution of the rest of this method until the
         // above API call is returned.

      } 

      //Do the makeGetForTweetsRequestThatRequiresToken stuff
}

我认为您的令牌API将有回调。您可以注册一个块来处理对TweetsRequest API的回调:

typedef void (^TokenRequestCompletionHandler)(BOOL success, NSString *token);

-(void) requestTokenCompletionHandler:(TokenRequestCompletionHandler)completionHandler
{
    //Call your token request API here.
    //If get a valid token, for example error==nil
    if (!error) {
        completionHandler(YES,token);
    } else {
        completionHandler(NO,token);
    }
}
在您的推特请求中:

-(void)makeGetForTweetsRequestThatRequiresToken {
  if(nil == token) {

     // make another API call to get a token and save it
     // stop execution of the rest of this method until the
     // above API call is returned.
     [tokenManager requestTokenCompletionHandler:^(BOOL success, NSString *token){
         if (success) {
           //Do the makeGetForTweetsRequestThatRequiresToken stuff

         } else {
           NSLog(@"Token Error");
         }
     }];
  } else {
    //You have a token, just Do the makeGetForTweetsRequestThatRequiresToken stuff
  }
}

您有completionHandler(是,令牌);两次?我很抱歉。编辑!非常感谢。