ObjectiveC Blocks and Variables

孤街醉人 提交于 2019-12-11 03:58:46

问题


I have the following method that makes a webservice call from my iOS app (using Restkit)...

BOOL valid = NO;

RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSString *servicePath = [WebServiceHelper pathForServiceOperation:[NSString stringWithFormat:@"/security/isSessionValid/%@", username]];
[objectManager getObjectsAtPath:servicePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    BooleanServiceResponse *resp = [mappingResult firstObject];
    valid = resp.value;
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error while validating session for user %@ : %@", username, error);
}];

return valid;

However, I get an error on the valid variable, saying it is declared outside the block and is not assignable. I Googled around a bit, and found a recommendation that I declare valid like this instead...

__block BOOL valid = NO;

That gets rid of the error. However, I find that no matter what I set the valid value to within my block, it is not set appropriately when exiting the block. How can I set this value so my method returns the expected value?


回答1:


I think you don't understand how blocks work. It's not a matter of variable visibility, although __block is correct.

You block is a function executed asynchronously, so valid will be set to resp.value whenever that block is executed, which is very likely to happen later than your return statement.

You need to change your design since so far you are returning an object which is not guaranteed to be set.

EDIT

Example

- (void)didFinishValidation:(BOOL)valid {
   // Do whatever you like with the `valid` value
   if (valid) {
     //...
   } else {
     //...
   }
}

and your blocks become

[objectManager getObjectsAtPath:servicePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    BooleanServiceResponse *resp = [mappingResult firstObject];
    valid = resp.value;
    [self didFinishValidation:valid];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error while validating session for user %@ : %@", username, error);
}];



回答2:


In general it's required to define variables as __block in order to mutate them from within a block. The problem here is that the "return" is called before the block is called - the best way to handle this situation is not to define this method as - (BOOL)... but as - (void)... and return the result async via delegation or block callback.



来源:https://stackoverflow.com/questions/14082775/objectivec-blocks-and-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!