Stop iOS from caching your data requests with HTTPClient – Appcelerator

If you haven’t noticed, iOS will cache your HTTPClient calls for a VERY long time. Sending a GET request for a file can sometimes return data that is weeks old. How can you guarantee your app will get the latest and greatest data? Structure your HTTPClient calls as such:

var xhr = Titanium.Network.createHTTPClient();
xhr.clearCookies(''); //not always needed, but may as well clear'GET', '');
xhr.setRequestHeader('Cache-Control','no-cache'); //Important bit
xhr.setRequestHeader('Cache-Control','no-store'); //Important bit

xhr.setRequestHeader(‘Cache-Control’,’no-cache’) &  xhr.setRequestHeader(‘Cache-Control’,’no-store’) will ensure you get the newest file from your server. The order seems to matter, as placing these before will still return cached data. Put the setRequestHeaders between .open and .send.

This doesn’t seem to be an issue with Android with or without this code, but again, it can’t hurt to use it to make sure you’re getting up to date information from your server.


Leave a Reply

%d bloggers like this: