Tuesday, January 20, 2009

XML and Flash Lite..... stuff of nightmares

XML has long plagued Flash Lite developers. It really is the stuff of nightmares, if you use XML you enter a world where nothing is what it seems. Your application lives or dies on the whim of Flash Lite and the XML document itself. If you are not careful you will find that the XML object is not cleared by Garbage Collection, scripts will reach the script timeout limit and stop processing silently, your application will fail.

After years of suffering here a few short notes on successful use of XML.

1. IdMap locks an XML object in memory. This is a bug. Here's the issue, with some XML files it is impossible to delete the XML object from memory because somehow the idMap holds a reference to the XML object itself. So before deleting an XML object you must first delete the XML.idMap property.

2. If you want to avoid the potential pitfalls of creating and then deleting XML objects, try just reusing the same XML object to process all your files.

3. Never do too much in a loop, this can cause the FL player to freak out and stop processing the loop, leaving you with an unfinished set of data. The timeout for loops on desktop is 15 seconds, on mobile this seems to be a lot less. This could be because OEMs have set it lower or it could just be because of poor implementations - who knows!

4. Don't forget the UI. Flash is single threaded so when you are executing code Flash must finish before it can render. This means your UI won't respond if actionscript is busy parsing an XML file.

5. onData() and onLoad() happen in the same frame. This means when an XML file is loaded it first creates the XML object via parseXML() then it calls onLoad() where you can walk through the DOM and access the nodes. The implication of this is that Flash has 2 heavy bits of processing to do in one frame, increasing the likelihood of a timeout. If timeouts are an issue you can overwrite the onData() event, call parseXML() and then call onLoad() in the next frame.

6. If you control the data you use then process it into something other than XML on the server or locally. XML objects occupy a lot of memory, name value pairs are much more efficient and you wont need to process them at all.

7. Test your XML loading needs at super speed in device central. So take your basic requirements for XML processing, create a new fla and set the FPS to 120. Do your loading operation 1000s of times and look at the memory. You'll get a good idea how good your memory handling is.