0

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?

DMonkey
  • 195
  • 2
  • 13
  • If you inserting data for the first time, an obvious saving would be using `createObjectFromJson` instead of `createOrUpdateObjectFromJson`. Calling `createOrUpdate` will do a query for existing elements, which will be slower. – Christian Melchior Sep 19 '17 at 09:02
  • Hi Christian, thanks for the response, using only createObjectFromJson seems to roughly shave 1-2 ms per object (not always), which is an improvement but a drop in the ocean compared to the total time spent in the method. – DMonkey Sep 19 '17 at 10:38
  • Have you tried converting the the JSON to typed classes using e.g. GSON and then using `realm.insert` instead? JSON is highly dynamic, so we need to have a lot more checks compared to working with Java classes where we can rely on the type system catching most bugs. – Christian Melchior Sep 20 '17 at 07:07
  • Thanks for the reply Christian, I just tried with realm.insert and it is more or less 25 seconds insert time for the 1000 objects. That of course is an improvement, however the 25 seconds of gson parsing into my object list is not desirable in terms of memory nor performance. This sets off the GC every 5 seconds. I also do post treatment to the object returned in createOrUpdateObject by creating relations to other objects using annotations, this is not included in the 60ms per object just for reference. – DMonkey Sep 20 '17 at 09:49
  • No matter what it shouldn't take that long to insert data into Realm. Calling `commit` is normally, by far, the biggest offender when measuring performance. Can you by any chance reproduce this in a sample project you can send our way so we can take a look at it? – Christian Melchior Sep 20 '17 at 20:14
  • Hi Christian, Sent an email to help at realm dot io with a link to this question, short explanation and a zip of the test android project. Thanks. – DMonkey Sep 21 '17 at 13:21

0 Answers0