Is using ByteArray.writeObject() a good way to save state?
Hi All,
I'm thinking of switching from using SharedObject for saving state to using ByteArray.writeObject() then saving the ByteArray instance as a file.
I found some code on the Starling forum that does this (see below). It looks fairly straightforward.
Have any of you tried this general approach and found it to be reliable?
Or, tried it and found out that there are "gotchas"?
I don't need to encrypt my data - just need to save and retrieve strings, numbers, and arrays of strings and numbers.
All feedback welcome ![]()
Thanks,
Douglas
Sample code by "andtankian":
registerClassAlias("MyClass", MyClass);
var file:File; //To point to a file in local storage
var fs:FileStream; //To operate the stream action
var bytes:ByteArray; //Container to your serialized class
bytes = new ByteArray();
file = File.applicationStorageDirectory.resolvePath("the path you wanna save it");
bytes.writeObject(entity); //entity is the object class containing the data you wanna save (a MyClass object)
//don't forget you cannot save any subclasses which extend DisplayObject.
fs.open(file, FileMode.WRITE);
fs.writeBytes(bytes);
fs.close()
fs.open(file, FileMode.READ);
fs.readBytes(bytes);
entity = bytes.readObject() as MyClass;
fs.close();
