Question

I'm tring to use the etag to cache my images, the first time it downloads all the images like it should however the second time it does too when it should enter the failed block with 304.

I've tried making the request externally and I get 304 like I should, it's just with AFNetworking that i'm having trouble

   NSString *urlString = [API_BASE_URL_PHOTOS stringByAppendingPathComponent:[photo getPathToPhoto]];
    NSString *properlyEscapedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:properlyEscapedURL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    if ([UserDefaultManager getEtagForKey:photo.nom] != nil) {
        [request setValue:[UserDefaultManager getEtagForKey:photo.nom] forHTTPHeaderField:ETAG_IF_NONE_MATCH];
    } 
    [request setHTTPMethod:API_METHOD_GET];

AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

    [FileUtils createDirectoryInDocumentsWithFileName:photo.folderName];
    NSString *docPath = [FileUtils getPathInDocumentsWithFileName:[photo getPathToPhoto]];

    // Save Image
    NSData *imageData = UIImageJPEGRepresentation(image, 90);
    [imageData writeToFile:docPath atomically:YES];


    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *allHeaderFields = [response allHeaderFields];
        [UserDefaultManager setEtag:[allHeaderFields objectForKey:ETAG] forKey:photo.nom];
    }


    if ([[DownloadManager sharedManager].downloadQueue.operations count] == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_ALL_PHOTOS_FINISHED object:nil];
    }
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    if (response.statusCode != RESPONSE_CODE_PHOTO_UP_TO_DATE) {
        LogDebug(@"%@", [error localizedDescription]);
    }

}];
Was it helpful?

Solution

I managed to fix this by changing my request to

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                           timeoutInterval:60];

hope that this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top