Stop iOS from caching your data requests with HTTPClient – Appcelerator

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('http://www.whateveryourURLis.com'); //not always needed, but may as well clear 
xhr.open('GET', 'http://www.whateveryourURLis.com');
xhr.setRequestHeader('Cache-Control','no-cache'); //Important bit
xhr.setRequestHeader('Cache-Control','no-store'); //Important bit
xhr.send();

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 xhr.open() 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