2

I have an application that makes signed requests to my server. All of the requests require an authentication token. When ever I make a request, if an auth token was not found, I store that request using an NSInvocation, query for the authentication token, invoke my invocation, then return back to the original calling method with a completion block.

My question is, how do you correctly pass a return block into an NSInvocation? I have no errors, but for some reason the original caller of [someClass listFilesWithCompletionBlock]'s completion block never triggers.

...
 [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];
        }
    }];
Ngoan Nguyen
  • 747
  • 2
  • 9
  • 19

0 Answers0