Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

ios - can I manually choose to get data from response cache or from server in Alamofire?

I am new in using Alamofire and iOS development in general. I am fetching data from server using the code below

func getDataFromServer(completion: @escaping ( _ dataString: String) -> Void) {
        
        
        let url = "http://app10.com/index.php/Data"
        let parameters : [String:Any] = ["someData": "xxx"]
        AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers:nil).responseJSON { (response) in
            
            switch response.result {
            
                case .failure(let errorResponse) :
                    
                    let errorMessage = "error message here: (errorResponse.localizedDescription)"
                    print(errorMessage)
                    completion("")
                    
                case .success(let value) :
                    
                    let json = JSON(value)
                    let dataString = json["data"].stringValue
                    completion(dataString)
            }
            
        }
        
    }

as you can see from the code above, I am trying to get dataString from server. but what I want to achieve is something like this

if meetSomeCondition  {
    // get 'dataString' from response cache from alamofire
} else {
    // fetch 'dataString' from server, using the code above
}

can I do something like that using only alamofire?

I am sorry, I am newbie, but I can do something like that if I use Firebase Firestore, so maybe alamofire has feature something like that

I have tried to search but I can't find it in Stackoverflow


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The underlying URLSession utilises a URLCache. Ensure your URLSession uses a URLCache and your server uses an appropriate response to instruct the client to cache the response.

In order to get the cached response, if any, the method according HTTP would be to set a cache control request header:

Cache-Control: only-if-cached (see RFC 5.2.1.7 only-if-cached)

That is, you would need to set this additional header when you create the request. Then execute the request as usual and you would either get the cached response or a status code 504 if there is none. Exactly what you would need.

Now, when you try this you realise, it unfortunately won't work as expected (bummer!). The reasons for this is manifold and it would be futile to go into detail.

The approach you may try next is to set the cache control policy of the URLRequest as follows:

urlRequest.cachePolicy = .returnCacheDataDontLoad

You can look up the meaning of it here: returnCacheDataDontLoad

Please read the documentation and the source documentation as well carefully, since it's not implemented in every iOS version!

That seems to be a working solution, however when using Alamofire you need to access the underlying URLRequest in order to set the cache policy (you may search on SO how to accomplish this) - and you need to be sure Alamofire does not alter your cache policy under the hood.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...