codenameone - 将包含 ListProperty 的 PropertyBusinessObject 存储到 JSON
问题描述
我很难将PropertyBusinessObject
包含一个ListProperty
. 我创建了以下测试用例,它在第一次执行时运行良好,但在第二次执行时失败。我的代码错了吗?
我的测试用例由四个类组成:
测试用例JSON
public void init(Object context) {
[...]
DB.initPersistenceDB();
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
DB.userDB.uniqueID.set("1");
DB.userDB.authToken.set("myToken");
InjuryDAO newInjury = new InjuryDAO();
DB.userDB.injuries.add(newInjury);
DB.saveDB();
}
D B
public class DB {
// User Database
public static UserDB userDB = new UserDB();
/**
* Call this method in the init(), it allows the database persistence and
* automatically restores the saved DB.
*/
public static void initPersistenceDB() {
Log.p("DB.initPersistenceDB executing", Log.DEBUG);
restoreDB();
}
/**
* Saves the UserDB to the Storage
*/
public static void saveDB() {
Log.p("DB.saveUserDB executing", Log.INFO);
userDB.getPropertyIndex().storeJSON("UserDB");
}
/**
* Restore the UserDB from the Storage, if it was previously saved to; this
* method is called by initPersistenceDB()
*/
private static void restoreDB() {
Log.p("DB.restoreUserDB executing", Log.INFO);
if (Storage.getInstance().exists("UserDB")) {
userDB.getPropertyIndex().loadJSON("UserDB");
}
}
}
用户数据库
public class UserDB implements PropertyBusinessObject {
public final Property<String, UserDB> uniqueID = new Property<>("uniqueID", null);
public final Property<String, UserDB> authToken = new Property<>("authToken", null);
public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries");
public final PropertyIndex idx = new PropertyIndex(this, "UserDB",
uniqueID, authToken,
injuries
);
@Override
public PropertyIndex getPropertyIndex() {
return idx;
}
}
伤害道
public class InjuryDAO implements PropertyBusinessObject {
// extra info
public final LongProperty<InjuryDAO> injuryCreationTime = new LongProperty<>("injuryCreationTime", System.currentTimeMillis());
// mandatory info
public final Property<Date, InjuryDAO> dateInjury = new Property<>("dateInjury", Date.class);
private final PropertyIndex idx = new PropertyIndex(this, "InjuryDAO",
injuryCreationTime, dateInjury
);
@Override
public PropertyIndex getPropertyIndex() {
return idx;
}
}
在Simulator中执行之前,我清空了.cn1
目录(即相当于Simulator,Clean Storage)。
第一次执行后,这是日志:
[EDT] 0:0:0,110 - DB.initPersistenceDB executing
[EDT] 0:0:0,110 - DB.restoreUserDB executing
[EDT] 0:0:0,178 - DB.saveUserDB executing
这是目录UserDB
内文件的内容.cn1
:
{
"injuries": [{"injuryCreationTime": 1558202520667}],
"authToken": "myToken",
"uniqueID": "1"
}
这是正确的。
第二次执行后,日志与前次相同,但这是UserDB
文件内容错误(注意前次时间戳丢失):
{
"injuries": [
{"injuries": []},
{"injuryCreationTime": 1558202625655}
],
"authToken": "myToken",
"uniqueID": "1"
}
预期的正确结果应该是:
{
"injuries": [
{"injuryCreationTime": 1558202520667},
{"injuryCreationTime": 1558202625655}
],
"authToken": "myToken",
"uniqueID": "1"
}
谢谢您的支持。
解决方案
我认为这:
public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries");
应改为:
public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries", InjuryDAO.class);
如果这不起作用,请尝试:
public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries", InjuryDAO.class, null);
推荐阅读
- python - python RSOME中决策变量数组和整数列表的元素乘法
- python - 动画 Python Kivy
- flutter - 未成功将事件添加到日历
- python - 如何使用 XOR 运算符比较二进制字符串的 2 个输入列并将输出分配给新列(熊猫)
- aws-lambda - AWS SQS 列出来自 SDK 的触发器
- python-3.x - 寻找带有 uclibc、gcc 和希望带有 pip 的 Linux docker 映像,如果没有,我可以从源代码安装它们
- django - 如何覆盖现有的 '/api/products/' 以在 Django-osacr-api 中返回更多字段?
- python - 程序停止工作 pyaudio、speechrecognition 和 pyttsx3
- python - 为什么statsmodels acf函数对scipy的pearsonr给出不同的答案?
- sql - 如何在 Spark SQL 中执行正则表达式