ObjectiveC Blocks and Variables

2019-07-20 02:40发布

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?

2条回答
beautiful°
2楼-- · 2019-07-20 03:05

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);
}];
查看更多
干净又极端
3楼-- · 2019-07-20 03:31

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.

查看更多
登录 后发表回答