2
votes

I'm using AFNetworking in my project as network layer. For images I had some problems related to the caching and I updated the framework to 3.0 version.

During the update process I have some problems with requests which fetch the media content (images and videos).

This is my class :

+ (void)requestWithMethod:(NSString*)methodTypeValue path:(NSString*)pathValue parameters:(NSDictionary *)params token:(NSString*)token debug:(BOOL)debug completionHandler:(void (^)(id result, NSError *error))block {
    if(debug){
        NSLog(@"Method Type  = %@ \n",methodTypeValue);
        NSLog(@"Link = %@ \n",pathValue);
        NSLog(@"Token = %@", token);
    }

    NSString *linkRequest = [NSString stringWithFormat:@"%@%@",GatewayRoot,pathValue];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:methodTypeValue URLString:linkRequest parameters:nil error:nil];

    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    if (token) {
       [req setValue:token forHTTPHeaderField:@"AccessToken"];
    }

    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
                if (debug) {
                    NSLog(@"response:\n %@ \n",responseObject);
                }
            block(responseObject,nil);
        } else {
            NSLog(@"Error: %@, %@, %@", error, response, responseObject);
            block(nil, error);
        }
    }] resume];
}

I made test and everything is fine related to the normal requests. I have to fetch some images and videos from server and I receive this error :

NSLocalizedDescription=Request failed: unacceptable content-type: image/png NSLocalizedDescription=Request failed: unacceptable content-type: video/mp4

I tried to set the content types in this ways :

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"image/jpeg",@"video/mpeg",...nil];

manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"image/jpeg",@"video/mpeg",...nil];
  1. Please, can you help my how to set the acceptable content types ?

  2. I have other problem related to image caching: with old version I loged in and I fetch the user profile image and after I loged out and relogin with other credentials I received the same image for user profile (I found that it was an issue realted to AFNetworking caching images).

Can you help me with the clear cache request flow ?

Thank a lot for your help !

1
Unrelated to your question, if you use AFJSONRequestSerializer, I don't believe you need to set the Content-Type yourself. I believe AFNetworking does that for you.Rob

1 Answers

5
votes

Rather than setting the acceptable content types yourself, you probably just want to specify the appropriate responseSerializer. For example, if you want the UIImage, you can do:

manager.responseSerializer = [AFImageResponseSerializer serializer];

If you want the NSData, you can do:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

If you don't want your request to be cached, you should set the req.cachePolicy to NSURLRequestReloadIgnoringCacheData or implement AFURLSessionManager cache response block, setDataTaskWillCacheResponseBlock. Or, of course, you could change your server so that those resources that shouldn't be cached have their header fields set accordingly.