azamsharp
azamsharp

Reputation: 20068

Block not fired

I have the following code and for some reason my block is not fired.

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        return 0; 

        HttpRequestHelper *requestHelper = [[HttpRequestHelper alloc] init];

        [requestHelper processRequest:@"www.yahoo.com" callback:^(NSString * response) {

            NSLog(@"%@",response);

        }];


    }

}

@implementation HttpRequestHelper

-(void) processRequest:(NSString *)url callback:(void (^)(NSString *))block
{
    block(url); 
}

Upvotes: 0

Views: 52

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

Well, first of all, you have return 0; right at the start of main(), so it's going to immediately exit.

Even after that, from its interface, I gather than HttpRequestHelper is asynchronous. It will start processing the request, but won't immediately call the callback. Rather, it will only call the callback after the request has been processed. The problem is, you don't give it an opportunity to process the request. As soon as you've initiated the request, you allow main to exit, which terminates the program.

You probably have to run the run loop until the callback is called.

Upvotes: 1

Related Questions