2

我有一个应用程序向我的服务器发出签名请求。所有请求都需要身份验证令牌。每当我发出请求时,如果未找到身份验证令牌,我会使用 NSInvocation 存储该请求,查询身份验证令牌,调用我的调用,然后返回带有完成块的原始调用方法。

我的问题是,如何正确地将返回块传递给 NSInvocation?我没有错误,但由于某种原因,[someClass listFilesWithCompletionBlock] 的完成块的原始调用者永远不会触发。

...
 [someClass listFilesWithCompletionBlock:^(id response, id error){
             onCompletion(response, error);  // <-- never returns here
  }];

...

// someClass
// method that is called if token was valid
- (void) someMethodWithCompletionBlock:(void(^)(id response, id error))onCompletion{
     // do the work that lists all of the files from server
    onCompletion(response, error);

}

// if token was valid, call someMethod, if not then set the invocation, then call fetch token
- (void) listFilesWithCompletionBlock:(void(^)(id response, id error))onCompletion{
    onCompletion(response, error){

    if(token){
        [self someMethodWithCompletionBlock:^(id response, id error){
             onCompletion(response, error);
         }];

    }else{
            void (^blockName)(id, id) = ^void (id response, id error){
                   NSLog(@"block!");
             };

        SEL selector = @selector(listFilesWithCompletionBlock:);
        NSMethodSignature *signature  = [self methodSignatureForSelector:selector];
        self.invocation = [NSInvocation invocationWithMethodSignature:signature];
        [self.invocation setTarget:self];
        [self.invocation setSelector:selector];
        [self.invocation setArgument:&blockName atIndex:2];

        [self fetchToken];          

    }

}


    // fetches token, then invokes
    - (void) fetchToken{
        [anotherClass fetchTokenWithCompletionBlock:^(NSString *responseToken, id error) {
        if(responseToken){
            // we got the token
            // invoke any methods here
            // should call listFiles with valid token then calls someMethod
            [self.invocation invoke];
        }
    }];
4

0 回答 0