all.
I have a method:
-(void)connectToServer:(NSError**)error{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//bla bla bla code;
*error=[NSError bla bla bla];
}
}
But after that error variable no modificated. Its no block variable. How can i modify this method for mark (NSError**)error as __block variable. If __block varibale used only for local variables?
Now i solve the problem with organization local variable with __block attribute:
-(void)connectToServer:(NSError**)error{
__block NSError *localError=*error;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//bla bla bla code;
localError=[NSError bla bla bla];
}
*error=localError;
}
But this solution not like my, because i need redeclare __block NSError *localError=*error;
Mu.
What you’re doing doesn’t make much sense. The
connectToServer:method is quite likely going to return before that block sets theerrorvariable, which means the caller isn’t going to have a valid object to look at.You need to have the block call another method to handle any errors that result, or run the block synchronously so that
connectToServer:doesn’t return before the block has finished.Also, in the second case you should be following Cocoa convention for error handling and indicating that an error occurred via the direct return of the method.
This should be rewritten as either:
Or: