How can I retrieve a return value from a completion block?

You’re missing some basics about asynchronous development with blocks. You can’t have a dispatched block return from anywhere but its own scope. Think of each block as its own method, instead of inline code.

I think what you’re looking for is something similar to this…

- (void)testWithHandler:(void(^)(int result))handler
{
    [obj somemethodwithcompeltionblock:^{
            int someInt = 10;
            dispatch_async(dispatch_get_main_queue(), ^{
                handler(10);
            });
      }
      ];
}


- (void)callSite
{
    [self testWithHandler:^(int testResult){
        NSLog(@"Result was %d", testResult);
    }];
}

Leave a Comment