My android app saves an array of Objects to cache using serializable, but when updates are released, the array is completely deleted and users have to re-do all input. What is the solution to this? Thanks in advance
我的Android应用程序使用serializable保存一个对象数组进行缓存,但是当发布更新时,数组被完全删除,用户必须重新进行所有输入。这是什么解决方案?提前致谢
2 个解决方案
#1
How did you store you serializable object to the cache? The package management system will keep the old data for an update installation of the app.
你是如何将可序列化对象存储到缓存的?包管理系统将保留旧数据以进行应用程序的更新安装。
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(
getCacheDir() + File.separator + "object"));
MyObject object = new MyObject();
object.setData(15);
object.setName("name");
objectOutputStream.writeObject(new MyObject());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#2
Look into ActiveAndroid if you are fine with storing data on the device. Look into building out a restful API in the cloud if you want it to persist forever.
如果您可以在设备上存储数据,请查看ActiveAndroid。如果您希望它永远存在,请考虑在云中构建一个宁静的API。
#1
How did you store you serializable object to the cache? The package management system will keep the old data for an update installation of the app.
你是如何将可序列化对象存储到缓存的?包管理系统将保留旧数据以进行应用程序的更新安装。
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(
getCacheDir() + File.separator + "object"));
MyObject object = new MyObject();
object.setData(15);
object.setName("name");
objectOutputStream.writeObject(new MyObject());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#2
Look into ActiveAndroid if you are fine with storing data on the device. Look into building out a restful API in the cloud if you want it to persist forever.
如果您可以在设备上存储数据,请查看ActiveAndroid。如果您希望它永远存在,请考虑在云中构建一个宁静的API。