Flex mobile local storage.

aduvall
aduvall used Ask the Experts™
on
I have a simple mobile app for ios.  The app contains 2 text fields and a submit button. I have written so that data input into the fields is saved to MySQL db.  It works fine as long is there is network coverage but of course when there is none you can't save the data.  My question is how can I save this data locally then upload when network coverage is available?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi

Your would store yout data in localStorage, if you have complex data to save it makes sense to use a structured format, I chose XML in the following example

// you need these imports
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

// add the fields you want to store to this structure
var settings:XML =
                  <settings>
                        <myData>false</myData>
                        <moreData>123</moreData>
                  </settings>;

// save settings - save your data here when you are offline
settings.myData = "Update me with the new data";
settings.moreData = 999;

var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("settings.xml");

var stream:FileStream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.truncate();
stream.writeUTFBytes( settings.toString());
stream.close();

/// read data back - do this when you are back online
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("settings.xml");
if( prefsFile.exists)
{
      var stream:FileStream = new FileStream();
      stream.open(prefsFile, FileMode.READ);
      settings = XML(stream.readUTFBytes( prefsFile.size));
      stream.close();
}
//use the data from the XML object here. eg:
// sendDataToServer( settings.myData);
// sendDataToServer( settings.toString()); // to send the whole XML object

Regards
Gary

Author

Commented:
Perfect this answer helped me.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial