Realm insertion of JSON object takes 60ms per obje

2019-08-29 09:20发布

问题:

I have the following json object : https://pastebin.com/B9Z1Wmqd

Currently using Realm 2.0.2

My relevant objects (simplified) are as follows so you can see the mapping :

Top level object:

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;
}

Sub objects :

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;
}

I am using a Motog G 1st Gen phone and doing some performance tests (still a very decent phone). The average time for this and similarly complex objects of the same type to be inserted is roughly 60ms per PlannedTaskDao object inserted. The DB is empty, so it is not an update. I performed this on a loop of 1000 objects in 1 transaction.

To parse the json string of these objects from the web service response into a JSONArray takes ~2secs to give an order of magnitude.

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();
      }
 }

This obviously means that it takes roughly 1 minute to insert 1000 objects.

Is this normal for realm/realm-java to have this performance? Is there anything I can do to improve the performance to reduce the insertion time into realm? I am not using the latest version, has there been any big advances in the latest version of realm java to improve this?