Android

realm autoincrement

aucd29 2016. 4. 29. 10:57
http://sys1yagi.hatenablog.com/entry/2015/08/14/131226

public class AutoIncrement {
public static long newId(Realm realm, Class<? extends RealmObject> clazz) {
    return newIdWithIdName(realm, clazz, "id");
}

public static long newIdWithIdName(Realm realm, Class<? extends RealmObject> clazz, String idName) {
    return realm.where(clazz).maximumInt(idName) + 1;
}
}

realm.beginTransaction();
Item item = realm.createObject(Item.class);
item.setId(AutoIncrement.newId(Item.class));
// 略
realm.commitTransaction();