我有以下json对象:https://pastebin.com/B9Z1Wmqd
目前正在使用Realm 2.0.2
我的相关对象(简化)如下所示,您可以看到映射:
顶级对象:
public class PlannedTaskDao extends RealmObject {
@PrimaryKey
private Long tasks_id;
private Date date;
private Date lastUpdate;
private Long clients_id;
private String hour;
private String libelle;
private String comments;
private Boolean closed;
private int displayOrder;
private long executionTime;
private Long locations_status_id;
private boolean deleted;
private String locationName;
private RealmList<PlannedTaskGroupDao> tasks;
private RealmList<PlannedTaskUserDao> users;
private Long affected_users_id;
}
子对象:
public class PlannedTaskGroupDao extends RealmObject {
private Long group_config_id;
private String libelle;
private RealmList<PlannedSubTaskDao> subtasks;
}
public class PlannedSubTaskDao extends RealmObject {
private Long tasks_id;
private String task_value;
private String libelle;
private String type; // status_value/status_only
private Long status_id;
private SiteDao site;
private Long controls_items_id = 0L;
private float weight;
private Long subtask_config_id;
}
public class PlannedTaskUserDao extends RealmObject {
private String config_user_id;
private Long chosen_user_id;
private String signature;
private boolean signed;
private String userName;
}
我正在使用Motog G 1st Gen手机并进行一些性能测试(仍然是一款非常不错的手机)。插入相同类型的此类和类似复杂对象的平均时间大约为每插入一个PlannedTaskDao对象60ms。数据库为空,因此不是更新。我在一次交易中在1000个对象的循环上执行了此操作。
要将这些对象的json字符串从Web服务响应解析为JSONArray需要~2secs来提供一个数量级。
JSONArray jsonArray = getResFromWS(); // contains 1000 objects of similar complexity to the pastebin json object shown above
for (int i = 0 ; i < jsonArray.length(); i++) {
try {
// calculate start time here
RealmObject realmObj = realm.createOrUpdateObjectFromJson(clazz, jsonArray.getJSONObject(i));
// check insertion time here in ms since start time
// do post treatment to realmObj
linkObjects(realmObj);
} catch (Exception e){
e.printStackTrace();
}
}
这显然意味着插入1000个对象大约需要1分钟。
对于realm / realm-java来说这是否正常? 我有什么办法可以改善性能以减少插入时间吗? 我没有使用最新版本,最新版本的realm java有没有大的进步来改善这个?