

This means that anyone processing the response should cache the response for 30 seconds at the most before requesting fresh data, as discussed previously. We get back a header in the response of Cache-Control: max-age=30. If we hit the URL below, you will get a response with the specified headers that you send in the URL query params. To test our example we are going to use the response headers service that is provided, this allows us to specify headers and values in the url query string and have them played back to us in the actual response headers. This is a service that allows you to send various URL parameters to it and have postman reply those items back to you in a certain response format. One of the services that are provided by Postman is something called Postman Echo. You may be using this already, it’s a fantastic tool for developing and testing APIs. To demonstrate this behaviour in action we are going to use a tool called Postman. It is possible to override this behaviour if you wish using one of the other options. In the case of the above it will cache request responses for the time specified in the header. This means they will honour the HTTP caching headers when making requests. After that period of time the response should be considered stale and new data be fetched from the source.īy default URLSession and URLRequest have a cache policy of useProtocolCachePolicy. This is the length of time in seconds that the receiver should consider this information valid for. Most APIs will specify something called a max-age in the response headers. The part of this statement that talks about maximum age before expiring is what we will explore here. Policies include how a resource is cached, where it’s cached and its maximum age before expiring (i.e., time to live) We won’t cover them all but the one we are most interested in for this example is the Cache-Control header.Ĭache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. These provide information and advice to the receiver about the response to the request. Now, most request to an API will return a bunch of HTTP headers. To help explain this we need to explore how HTTP caching headers are intended to work.

However what if I told you there is something simpler that is already built in to iOS as standard? Caching Headers

Now you could build some custom caching layer that writes things to disk or a library such as PINCache. This is just a sample of the challenges faced when trying to use a relational database as a caching layer for your app.
Iswift request server timeout update#
What happens if multiple requests need to update the data? Handling concurrent data updates carries it’s own complexity and head aches.Not only do you need to make sure you can parse and store the data correctly, you then must make sure you query the data in the same way so that you get the same results as returned from the API.What happens when the structure changes? Do you need to update your parsing and data structure. Whether you are mapping against a back end database or just the returned JSON.If you are consuming in an house API you may be trying to replicate a back end database structure driven by a much more capable server side DBMS.However they add a lot more complexity if you are simply using them as a caching layer. Now these solutions are fine if you are intending to levarage the power of a relational database to perform some kind of task. Popular solutions include putting an actual relational database inside your app to cache the data such as using Core Data or Realm. I hear of a lot of people put off by providing a caching layer in their apps for data.

Iswift request server timeout Offline#
In this post we are going to extend that simple networking example to include some offline caching so that if our network requests fail we can still provide content. Following on from the previous post where we explored simple JSON decoding.
