user3572524 user3572524 - 2 months ago 9
Android Question

Realm Auto Increament field example

I need to add auto increment key field in

Realm
database in android.
how can i do this?
Is this possible?

Thanks in advance.

N J N J
Answer

Relam currently doesn't support auto_increment

see this issue on GitHub

you can take work around like this

  realm.beginTransaction();
                dbObj obj = realm.createObject(dbObj.class);

                // increatement index
                int nextID = (int) (realm.where(dbObj.class).max("id").intValue() + 1);

                // insert new value
                obj.setId(nextID);