Realm-java: How to Create faster write transaction?

Created on 23 May 2016  Â·  14Comments  Â·  Source: realm/realm-java

I have the following code snippet mimic sqlite bulk insert for over 4000 transactions, I would like to know what should be the default fastest and alternative best method to perform block writes to Realm memory or persist to Realm files without any expensive overhead loading ? - See more at: http://www.mzan.com/article/33358880-realm-java-fastest-bulk-write-methodology.shtml#sthash.YmBNOR7f.dpuf

T-Help

Most helpful comment

@nhachicha he is using JSON import api

All 14 comments

copyToRealm uses createObject under the hood, so doing that manually is slightly faster (although not by much). copyToRealm is a lot safer though as that automatically handle duplicate objects and circular data structures. For inserting many objects I would recommend using copyToRealmOrUpdate. It is not as fast as we would like it yet, but we have room for a lot of improvement and it is being tracked here: #1684

I would like to know best practices for realm write transaction methods.Because, i'm doing synchronization 1 millions records from ERP server to android app.
I want to taking within 1 min for 5000 records.
Now, i'm using the follwing methods..

public T saveOrUpdate(Realm realm, T t) {
T result;
if (realm.isInTransaction() == false) {
realm.beginTransaction();
}
result = realm.copyToRealmOrUpdate(t);
realm.commitTransaction();
return result;
}

Thank....

It might be better to add objects in batches instead of do many small write transactions.

Realm realm = null;
try {
    realm = Realm.getInstance(config);
    realm.beginTransaction();
    RealmObj defaultInstance = new RealmObj();
    for(InputObj input : inputObjs) {
        defaultInstance = mapper.toRealmObject(realm, input, defaultInstance);
        realm.copyToRealmOrUpdate(defaultInstance);
    }
    realm.commitTransaction(); 
} catch(Throwable e) {
    Log.e(TAG, "An error occurred", e);
    try {
        realm.cancelTransaction();
    } catch(Exception e) {
        Log.w(TAG, "Probably not in a transaction.");
    }
    throw e;
} finally {
    if(realm != null) {
        realm.close();
    }
}

is what I do.

I'll close the issue. We are working on faster inserts (see #1684).

@Zhuinden I have this method:

public void insertOrUpdateJSON(InputStream inputStream, Class<? extends RealmModel> clazz){
        long time = System.currentTimeMillis();
        if (db.isInTransaction()) {
            db.createOrUpdateAllFromJson(clazz, inputStream);
        } else {
            try {
                db.beginTransaction();
                db.createOrUpdateAllFromJson(clazz,inputStream);
                db.commitTransaction();
            } catch (Exception e) {
                db.cancelTransaction();
                throw e;
            }
        }
        long end = System.currentTimeMillis() - time;
        Log.d("Base DAO","Tiempo de ejecucion insertar json by InputStream: "+ end);
    }

My JSON File have aproximately 100K values and for 3 files JSON took 2 minutes loading the data.
Exist another way to do it faster?

Well.... are you in transaction when this happens? Because one transaction is faster than many transactions.

This method is called by this action:

for(InputStream stream : streams) {
            catLocalidadesDAO.insertOrUpdateJSON(stream, CatLocalidadesInegi.class);
        }

But is it in a transaction???? :D

No, no is in a transaction :(, what do you suggest?

    try(Realm realm = Realm.getDefaultInstance()) {
        realm.executeTransaction(new Realm.Transaction() {
              @Override
              public void execute(Realm realm) {                        
                  for(InputStream stream : streams) {
                       catLocalidadesDAO.insertOrUpdateJSON(stream, CatLocalidadesInegi.class);
                  }
             }
        });
    }

Thanks a lot, it's true is faster using transaction

@andresvd1 have you also tried using insert* methods instead of copy*? copy* are generally good if you need to use/manipulate the managed object you just add to the Realm, whereas insert* are relatively faster since they don't return the managed object...

@nhachicha he is using JSON import api

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wezley98 picture wezley98  Â·  3Comments

yuwu picture yuwu  Â·  3Comments

AAChartModel picture AAChartModel  Â·  3Comments

mithrann picture mithrann  Â·  3Comments

harshvishu picture harshvishu  Â·  3Comments