1
votes

I have been developing a productivity app that uses a SharedObject. It saves custom classes as basic objects and variables. It works perfectly on my computer on the emulator but once I test it on my ipad 1st gen, it doesn't work. The app will background appropriately but when i close the background process and re open the app, all the entries are gone. I haven't payed the developer fee as I'm not sure if I want to invest yet, so I'm using a fake certificate and jailbroken device. Will that stop save data? Again it works exactly as need on pc, but when i close the app process on my ipad, it doesn't save anything.

EDIT: I FIXED IT Ok, the problem was simply that the flushes weren't calling on the regular changes due to scripting error, and the one on close, doesn't work when the app is closed from backgrounding. Basically I set it to save on home button, and on power button aswell as fixed all the little saves aswell. Works perfectly now. Files were too sketch cause I'm storing an array of custom classes.

1
Oh the joys of developing on iOS. >.> I'm not answering because I'm not 100% sure, but I think that SharedObjects are wiped out in garbage collection when the process ends. - CodeMouse92
Can you post the code where you save your shared object? I suspect that what is happening is its not getting called, rather than the shared object is broken. - Plastic Sturgeon
I call it on anytime I change a variable. I also call it again on close. There should be no way it isn't calling. I also flush it on close manually. I can't test with trace to see if flush worked cause it's on the ipad and flash cant do live diag on the jailbreak. I use NativeApplication.nativeApplication.addEventListener (Event.EXITS , onExitHandler) for the closing. And everything else is just when every something is changed, it re saves the data just in case. - user3167032
I actually feel like it might be because of the jailbreak. Because you can't install apps on the ios device without proper certificates and p12. Since i'm using fake to publish the ipa then jailbreaking and using appsync, maybe the ios isn't giving it permission. But if it could possibly be anything else I would love to know. I think it's really dumb that you need to pay 100 to develop and test your app. I understand for submission, but testing should be free... - user3167032
For what it is worth, SharedObject is generally avoided in AIR development in favor of File and FileStream. SharedObject has a 100kb limit, which really only makes it good for saving settings. And, even then, I personally prefer File since I get more control over it. - Josh

1 Answers

0
votes

To avoid adding to the comment chain, I'll just write an answer.

As I said in my comment, most AIR devs avoid using SharedObjects for various reasons. They are limited to ~100kb, you have no control over them, and apparently are flushed on iOS.

With that said, most AIR devs prefer to use File and FileStream which give you direct control over the device's file system.

FileStream features a method called writeObject() which does exactly what you want. Say you have the following strongly typed objects you wish to save (note: only public properties are saved to disk):

var a:Class1 = new Class1();
var s:Class2 = new Class2();
var d:Class3 = new Class3();
var f:Class4 = new Class4();

You'll want to combine them into a single object, or save them individually. For this, I will combine them into a single object.

// you could also use an array, or any other object, to do this
var obj:Object = {objA:a, objS:s, objD:d, objF:f}; 

Then you want to use File, which is a reference to a file or directory in the file system, and FileStream, which is what communicates with the file system, to write the object.

var f:File = File.applicationStorageDirectory.resolvePath("prefs.conf");
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeObject(obj);
fs.close() // NEVER forget to close()

And that's it.Your objects are now saved to disk. You can open them using File and FileStream again.

fs.open(f, FileMode.OPEN);
var obj2:Object = fs.readObject();
fs.close();

obj2 will have the same properties as obj and they will match the objects you originally saved. It is worth noting that these objects will not be of the same type as your custom objects. Unless they are primitives (Object, Array, String, Number, int, uint, maybe a few others), every object that is read from a Byte stream (whether it be ByteArray, FileStream, URLStream, etc) will be typed as a standard Object. You can get around this with registerClassAlias(), however.

EDIT: As a quick note, on iOS, you should always set File.preventBackup to true if they are anything other than preferences. Apple will reject it otherwise. The good news is, though, that those settings should be backed up to iCloud and persist between installations.