1

I am trying to build an ios app for our website. We have implemented api using oauth 2.0. Now, I am having an issue setting a property. I am using afnetworking2.0 for networking. Here is what I am trying to achieve:

I am writing functions that will allow me to check the validity of the access token or to get another access token using the refresh token. I have a boolean property that I declared in one of the header file that tells me if my access token is valid. Before making any request to the website I first make a request to check the validity of the access token using a function. In this function I set the value of the boolean property depending on whether the token is valid or not. Now, the problem is that I set this property inside the success or failure block of afnetworking2.0 post/get request's method and for some reason after making the function call if I print out the value of the boolean property, that I set in the function, I don't see the expected value. I have tried different approaches using __block local variables and then setting the boolean property to this local variable at the end of the function, but still no success. So, I am a little lost. All I am trying to do is implement a way of checking whether the access token is valid so that I can renew it before making a real request to the web server. Is there a better way of doing this? A second approach that I am planning to take is to store the expiry_date of the access_token and to check if the time has passed before making a request. In this scenario I save an extra request to the web server. I only make a renew request when my current time is passed the expiry time. But, again the issue I am facing here is setting the property in the success block. Here is the function that I am trying to implement:

- (BOOL) isTokenValid:(NSString *)accessToken
{
    __block BOOL tokenValid = NO;
    if (accessToken != nil)
    {
        static NSString *const baseUrl = @"baseurl";

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy.allowInvalidCertificates = YES;
        manager.requestSerializer = [AFJSONRequestSerializer serializer];

        [manager.requestSerializer setValue:[@"Bearer " stringByAppendingString:accessToken] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [manager GET:baseUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            tokenValid = YES;
            NSLog(@"Data: %@", responseObject);

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            tokenValid = NO;
        }];

    }
    else
    {
        tokenValid = NO;
    }

    self.tokenValid = tokenValid; // here I am setting the property, which I will be checking right after I call this function
    return tokenValid;
}

As you can see, this is one of the approaches that I am trying. Instead of setting the 'tokenValid' I also tried setting 'self.tokenValid' inside the block and didn't see any luck. So, how would I set the property properly inside the block?

Thank you

mdzahedhossain
  • 125
  • 1
  • 14

1 Answers1

1

There is no problem with setting the variable. It's just not set by the time the function returns because the operation is asynchronous.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Any idea how I can change the code to incorporate what I want? For now I have the 'self.tokenValid' as a property. But, soon I will change it to from being a property of this class to being the property of a singleton class. – mdzahedhossain Feb 18 '14 at 22:00
  • @mdhossain: You cannot expect it to be set by the time this function returns. Rather, you need to pass a completion handler to execute once the operation finishes and it has a value for tokenValid, and the completion handler can take the value of the tokenValid. The caller will put whatever it needs to do with tokenValid in the completion handler. – newacct Feb 18 '14 at 22:16
  • I actually understand what you meant by completionHandler after going through a tutorial about blocks and how they are used as completionHandler. The problem is solved. Accepting your answer. Thanks – mdzahedhossain Feb 21 '14 at 02:42