I recently ran into a problem in AIR, where my application wasn’t updating content it was getting from an HTTP request even though the page had changed.

I was able to use Charles Proxy to see that the application wasn’t even making the requests to the server, meaning it must be an issue with AIR rather than local or remote DNS, or web servers.

My requests were being made by the HTTPService class, and I couldn’t find any properties of methods that where related to caching, however I did find that URLRequest had two properties, useCache and cacheResponce.

 
var urlRequest:URLRequest = _new URLRequest();
urlRequest.cacheResponse = false;
urlRequest.useCache = false;

This is quite handy, although it didn’t seem to work for me, and also wasn’t idea for the situation I was in. After doing some digging on the web I found the classic solution of adding a random number to the end of the URL

 
 
 
 var urlRequest:URLRequest = _new URLRequest('http://www.example.com/');
 urlRequest.url += '?nocache=' + Math.random() * 100;

Again this isn’t ideal and could lead to problems, especially if you don’t know what server or URL the requests may be sent for. There is a chance the nocache (or any) variable could cause problems.

Eventually I noticed that the URLRequest’s cacheResponse entry on livedocs had a “See also” pointing to flash.net.URLRequestDefaults.cacheResponse. This solved all my problems, and is essentially a default for all URL requests made my AIR.

The properties appear to be static variables so you can set them like so:

1
2
3
4
 
// Turn caching off for AIR
URLRequestDefaults.cacheResponse = false;
URLRequestDefaults.useCache = false;

This only needs to be done once, so you can add it to your applications start up cycle.

This entry was posted on Friday, October 2nd, 2009 at 2:38 pm and is filed under AIR, ActionScript, Development, Flex. You can leave a comment and follow any responses to this entry through the RSS 2.0 feed.

Leave a Reply