evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

NSURLErrorDomain appear on some requests when in plane mode #31

Closed nmDejamobile closed 8 years ago

nmDejamobile commented 8 years ago

When on offline "plane mode", the following error appears and provides a Result.Failure enum type for response.result for the Alamofire request.

The strange thing is that previous request url offline cache works well on the same domain.

I tried to disable the previous request but the failing one still fails.

Any tips to debug ? I added breakpoints in Alamofire where errors are thrown, and also in EVURLCache.swift but I couldn't find where what throws this error. It looks to be lower level than EVURLCache or Alamofire

Note that all caches work well when in online mode.

---------------------
Request: POST http://mydomain/api
Headers: [
  Content-Type : application/json; charset=UTF-8
  Accept : application/json
  x-auth-token : 826fdd79-f2ca-456e-9620-80903a05c508:1504282475655:a7815835d4dd35cd01265204d1d42772
  Content-Length : 145
]
09/01/2016 18:41:02:301 myapp)[1205:.] EVURLCache.swift(104) cachedResponseForRequest:
    Returning cached data from /var/mobile/Containers/Data/Application/DAA4BA7E-9D8D-40F9-A6AB-04FE71E240F1/Documents/Cache/mydomain/api/index.html

---------------------
Error: The Internet connection appears to be offline.

the full error is : FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline."

My request code is the following :

        let paginatedRestServiceAddress = "mydomain/api"
        let token = "123456789"
        Alamofire.request(.POST, paginatedRestServiceAddress, headers: [
            "Content-Type": "application/json; charset=UTF-8",
            "Accept": "application/json",
            "x-auth-token":token
        ], parameters: [:], encoding: .Custom({
            (convertible, params) in
            let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
            mutableRequest.HTTPBody = Mapper().toJSONString(_pagination)!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            return (mutableRequest, nil)
        }))
            .validate(statusCode: [200])
            .responseString { (response: Response<String, NSError>) in
// the error appears at this moment
                debugPrint(response.result)
        }

and EVURLCache is loaded with :

        #if DEBUG
            EVURLCache.LOGGING = true // We want to see all caching actions
        #endif
        EVURLCache.filter { request in
            let url = request.URL!.URLString
            return !self.shouldUrlBeExcluded(url)
        }
        EVURLCache.activate()
evermeer commented 8 years ago

A cache result will only be returned if it's not expired. Could it be the case that sometimes this is the case? Because then the cache will be skipped and the normal request will be executed. Of course this request would require an internet connection.

When you do see this error, did you also see a call to the function cachedResponseForRequest? What dit it return and at what point? Set EVURLCache.LOGGING = true to see what is going on.

If a request has failed like this and you are positive that it's in the cache, you can still get the file by reading it from : EVURLCache.storagePathForRequest(theRequest)

evermeer commented 8 years ago

Is this still an issue?