I want to save my Array's strucure and load it the next time I open my AIR application. Is there a way to store it to an EncryptedLocalStore item then get it later when I re-open the app?
2 Answers
EncryptedLocalStore.setItem() method takes a byte array when storing contents. To store an array, just use ByteArray.writeObject() method (as described in http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html#writeObject()) to convert your Array to a ByteArray - and then persist the same to the ELS.
var array:Array = getArray();
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(array);
EncryptedLocalStore.setItem('somekey', byteArray);
Hope this helps.
Update: Added code to retrieve the array back.
var byteArray:ByteArray = EncryptedLocalStore.getItem('somekey');
var array:Array = byteArray.readObject() as Array;
Update: For custom classes.
In case you want to serialize your own custom classes to the ByteArray, you may have to call registerClassAlias() before writing the object to the ByteArray. For eg.
registerClassAlias("com.example.eg", ExampleClass);
-
I'll be curious to know if that works for you. I have had not had luck with that method in the past. Though I will point out that I was doing it 2+ years ago from an HTML/JS AIR application, so it's possible that I was doing it wrong, that it doesn't work the same with JS objects, or that it is a long-since-fixed bug. – Jason Dean Sep 30 '11 at 15:42
-
I'm quite new to byteArrays and plainly just arrays on as3 in general. Can you probably add a way for me to get the array back? If I analyzed it correctly, the method you posted is only for storing the array. – Registered User Sep 30 '11 at 17:28
-
2@JasonDean I have used this method before and it works for me. In case you were using a custom object to be saved, you would need to call registerClassAlias() before you convert the object to ByteArray. For example, registerClassAlias("com.example.eg", ExampleClass); – sangupta Oct 01 '11 at 04:31
I have found that it is easiest to to serialize the Array to a string and then store that string in the ELS. Then when you pull it out deserialize it back into an Array.

- 9,585
- 27
- 36
-
How do you actually "serialize" and "deserialize" the array? What functions to use? Etc. – Registered User Sep 30 '11 at 17:35
-
If you have as3corelib you can use that to serialize. http://ntt.cc/2008/10/06/as3corelib-tutorial-how-to-use-json-class-in-flex.html – Jason Dean Sep 30 '11 at 18:21