Note that there are some explanatory texts on larger screens.

plurals
  1. POSDURLCache with AFNetworking and offline mode not working
    primarykey
    data
    text
    <p>I'm using <code>AFNetworking</code> and <code>SDURLCache</code> for all my networking operations.</p> <p>I have <code>SDURLCache</code> set like this:</p> <pre><code>SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024*2 // 2MB mem cache diskCapacity:1024*1024*15 // 15MB disk cache diskPath:[SDURLCache defaultCachePath]]; [urlCache setMinCacheInterval:1]; [NSURLCache setSharedURLCache:urlCache]; </code></pre> <p>All my request are using cachePolicy <code>NSURLRequestUseProtocolCachePolicy</code>, which according to apple docs works like this:</p> <blockquote> <p>If an NSCachedURLResponse does not exist for the request, then the data is fetched from the originating source. If there is a cached response for the request, the URL loading system checks the response to determine if it specifies that the contents must be revalidated. If the contents must be revalidated a connection is made to the originating source to see if it has changed. If it has not changed, then the response is returned from the local cache. If it has changed, the data is fetched from the originating source.</p> <p>If the cached response doesn’t specify that the contents must be revalidated, the maximum age or expiration specified in the response is examined. If the cached response is recent enough, then the response is returned from the local cache. If the response is determined to be stale, the originating source is checked for newer data. If newer data is available, the data is fetched from the originating source, otherwise it is returned from the cache.</p> </blockquote> <p>So everything works perfectly even in airplane mode as long as the cache is not stale. When the cache expires (max-age and others), the failure block gets called.</p> <p>I've been digging a little inside the <code>SDURLCache</code> and this method returns a response with valid data (I've parsed the data to a string and it contains the cached information)</p> <pre><code>- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { request = [SDURLCache canonicalRequestForRequest:request]; NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest:request]; if (memoryResponse) { return memoryResponse; } NSString *cacheKey = [SDURLCache cacheKeyForURL:request.URL]; // NOTE: We don't handle expiration here as even staled cache data is // necessary for NSURLConnection to handle cache revalidation. // Staled cache data is also needed for cachePolicies which force the // use of the cache. __block NSCachedURLResponse *response = nil; dispatch_sync(get_disk_cache_queue(), ^{ NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kAFURLCacheInfoAccessesKey]; // OPTI: Check for cache-hit in in-memory dictionary before to hit FS if ([accesses objectForKey:cacheKey]) { response = [NSKeyedUnarchiver unarchiveObjectWithFile: [_diskCachePath stringByAppendingPathComponent:cacheKey]]; if (response) { // OPTI: Log entry last access time for LRU cache eviction // algorithm but don't save the dictionary // on disk now in order to save IO and time [accesses setObject:[NSDate date] forKey:cacheKey]; _diskCacheInfoDirty = YES; } } }); // OPTI: Store the response to memory cache for potential future requests if (response) { [super storeCachedResponse:response forRequest:request]; } return response; } </code></pre> <p>So at this point I have no idea what to do, because I believe that the response is handled by the OS and then <code>AFNetworking</code> receives a </p> <pre><code>- (void)connection:(NSURLConnection *)__unused connection didFailWithError:(NSError *)error </code></pre> <p>inside <code>AFURLConnectionOperation</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload