2012-12-22 16 views
8

Utilizzo AFNetworking nella mia app e cerco di farlo funzionare in modalità offline utilizzando i dati memorizzati nella cache, se disponibili.Modalità offline di AFNetworking (AFHttpClient) non funzionante con NSURLRequestReturnCacheDataDontPolitica di caricamento

Mi aspettavo dopo aver impostato il criterio di cache della richiesta su NSURLRequestReturnCacheDataDontLoad, getPath: parameters: success: failure: avrà successo con i dati memorizzati nella cache offline. Tuttavia, anche se ci sono dati nella cache (ho verificato controllando la cache con il codice), getPath fallirà semplicemente in modalità aereo.

C'era una discussione in gitub di rete AF: https://github.com/AFNetworking/AFNetworking/issues/378 Ma sembrava che il problema non fosse affatto risolto. L'autore di AFNetworking semplicemente puntare a Apple's document, e ha detto:

NSURLRequestReturnCacheDataDontLoad Specifies that the existing cache data should be used to satisfy a request, regardless of its age or expiration date. If there is no existing data in the cache corresponding to a URL load request, no attempt is made to load the data from the originating source, and the load is considered to have failed. This constant specifies a behavior that is similar to an “offline” mode.

Come ha detto Apple, NSURLRequestReturnCacheDataDontLoad è esattamente progettato per la modalità non in linea.

Sto testando su iOS6, ho provato sia con NSURLCache che con SDURLCache, tutti hanno lo stesso risultato.

Richiesta non riuscita, il messaggio di errore:

2012-12-22 03:11:18.988 Testapp[43692:907] error: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x211b87c0 {NSErrorFailingURLStringKey=http://Testapp.com/api/v1/photo/latest/, NSErrorFailingURLKey=http://Testapp.com/api/v1/photo/latest/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x211b9720 "The Internet connection appears to be offline."}

risposta

7

scoperto, si tratta di un bug in iOS 6.

C'è un thread di discussione in AFNetworking esattamente per questo problema: https://github.com/AFNetworking/AFNetworking/issues/566

Grazie per i suggerimenti e gli esperimenti di guykogus su questo argomento. Ho passato una notte su questo tema!

Un riassunto lavoro intorno viene letta la risposta dalla cache, invece di utilizzare la politica NSURLRequestReturnCacheDataDontLoad:

NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; 
if (cachedResponse != nil && 
    [[cachedResponse data] length] > 0) 
{ 
    // Get cached data 
    .... 
} 
Problemi correlati