我已经发现了这个问题的原因,而且 Adobe 似乎也发现并修复了这个问题(从 AIR 3.5 开始)。https://bugbase.adobe.com/index.cfm?event=bug&id=3347676
SharedObject 存储在根据您的应用程序使用的 SWF 命名的目录中,因此如果您的应用程序在“myApp.swf”下运行,SharedObject 将存储在目录“myApp”中。如果您更改此 SWF 的名称(即 AIR 项目的相应 XML 构建表配置文件),任何后续构建都将其 SharedObjects 存储在新位置。
此错误中描述的问题专门针对 iOS,其中应用程序没有将 SharedObject 存储在如上所述的相应 SWF 位置,而是存储在项目 XML 构建表中的“文件名”属性表示的单独位置。
我还发现,Adobe 确实容忍在移动平台上使用 SharedObject 进行持久存储。
我已经为将来的版本保存冗余开发了一个简单的备份,以防将来的更新无法持久保存 SharedObject。
/** the last timestamp a deep save was completed */
private var mLastSave:Number = -1;
/**
* save the state of the globals object and all of its
* sub objects.
* @warning
* all objects must implement variables in a "public" state.
* Private variables are not saved within the persistance manager
*/
public function save():void{
var so:SharedObject = SharedObject.getLocal("storage");
so.data.user = user;
so.flush();
saveDeep();
}
/**
* Save the SharedObject to denoted mobile applicationStorageDirectory.
*/
public function saveDeep():void{
// save on first application save or after
// every 5 minutes
if(mLastSave == -1 || mLastSave < getTime() + 300000){
mLastSave = getTime();
var file:File = File.applicationStorageDirectory.resolvePath("userSave");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var ba:ByteArray = new ByteArray();
ba.writeObject(user);
fileStream.writeBytes(ba);
fileStream.close();
}
}
/**
* Load the application from the SharedObject be default
* if the SharedObject DNE attempt to load from the
* applicationStorageDirectory, if neither exist
* create new User object
*/
public function load():void{
registerClassAlias("user", User);
// Create/read a shared-object named "userData"
var so:SharedObject = SharedObject.getLocal("storage");
var u:User;
if(so.data != null){
u = so.data.user;
}
trace("Loading application...");
if(u != null){
trace("LOADING SAVE DATA");
user = u;
}else{
trace("NO SAVE DATA EXISTS");
var file:File = File.applicationStorageDirectory.resolvePath("userSave");
if(file.exists){
trace("Found userSave backup -attempting to load");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var ba:ByteArray = new ByteArray();
fileStream.readBytes(ba);
fileStream.close();
try{
user = (ba.readObject() as User);
}catch( e : EOFError ){
trace("SharedObject did not exist, attempted userSave load, failed");
}
}else{
user = new User();
trace("created New user...");
}
}
}