Search code examples
iosafnetworkingafnetworking-2

Unwanted caching a GET request with AFNetworking


I have a situation here, I use rest API with AFNetworking to retrieve a specific resource (GET request) from our backend. When i check the backend with browser the result is 165 but in one specific device (one specific iPhone 6 plus) the result is 5, in any other device i retrieve 165 the correct answer. I think something cached the result on this device but doesn't found out anything on this specific device.

I deleted the application and installed it again and it works wrong again, so NSURLCache isn't the problem either

Code that i use to retrieve data from backend, it works fine any other devices, it doesn't work on that specific device. (by the way i use same username and password to check on all the devices and browsers) so this isn't the case.

AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

[manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults] valueForKey:TOKEN_ID_KEY] forHTTPHeaderField:TOKEN_HTTP_HEADER_NAME];
[manager GET:[NSString stringWithFormat:@"%@%@", BASE_URL, PROFILE_URL] parameters:nil success:^(AFHTTPRequestOperation* operation, id responseObject) {
    NSLog(@"profile info request accepted with response : %@",responseObject);
    profile = [[Profile alloc] initWithDictionary:responseObject];
    [self.tableView reloadData];
} failure:^(AFHTTPRequestOperation* operation, NSError* error) {
    NSLog(@"profile info request failed with eroor : %@" , [error localizedDescription]) ;
}];

Solution

  • If you find out nothing to solve your problem then you can add current time at the end of the url. It happened to me as well and I just added something extra to my url so that it's always a new request.

    long currentTime = (long)CFAbsoluteTimeGetCurrent();
    NSString *urlString = [NSString stringWithFormat:@"%@%@?%ld", 
                                                    BASE_URL, 
                                                    PROFILE_URL, 
                                                    currentTime];
    

    This is just a workaround. Hope this helps