Reputation: 19790
Suppose I have this kind function with a block inside it:
-(BOOL)checkSomething
{
server = [[Server alloc] initWith:privateVar];
[server checkSomethingWithCompletion:^(BOOL success){
//I want to return the value of success
}];
}
How do I go about returning the value that I get from the completion block? I'm not able to set variable from outside the block inside it. I'm also not able to return the value directly from the block.
Upvotes: 0
Views: 206
Reputation: 130092
If your checkSomethingWithCompletion:
method is running synchronously, declare a __block
variable before the block. You can then write to the variable from your block.
If your method is running asynchronously, checkSomething
will return sooner than checkSomethingWithCompletion
and therefore you cannot return anything from this block. In this case, you should use some asynchronous way to get the data, e.g. using notification, delegate or a callback method. The completion block could also be a parameter to your checkSomething
method.
Upvotes: 4