Autoincrement when adding record in Android Realm

0

Really I see that there is nothing like Mysql to be able to assign auto-increase of an index. How could something similar be done when entering new records.

Structure User extended RealmObject

public class User extends RealmObject {
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String TEAM_COLOR = "teamColor";
    public static final String LEVEL = "level";

    @PrimaryKey @Index
    private long id;

    private String name;
    private String teamColor;
    private int level;

    public long getId() { return id; }
    public void setId(long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getTeamColor() { return teamColor; }
    public void setTeamColor(String teamColor) { this.teamColor = teamColor; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
}

To insert data I do it in the following way:

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            User entry = realm.createObject(User.class);
            entry.setId(1); // aquí se indica el indice
            entry.setName("user1");
            entry.setTeamColor("red");
            entry.setLevel(23);
        }
    });
    
asked by Webserveis 02.09.2016 в 10:35
source

3 answers

1

Another option is to validate the null as if it were 0:

        User entry = realm.createObject(User.class);
        int idNext = realm.where(User.class).max(User.id).intValue();
        if (idNext != null) {
            entry.setId(idNext + 1); // aquí se indica el indice
        } else {
            entry.setId(1); // aquí se indica el indice
        }

        entry.setName("user1");
        entry.setTeamColor("red");
        entry.setLevel(23);
    
answered by 05.09.2016 / 19:51
source
2

Well I have another alternative, something simpler ..

public static int getUltimoId() {
    Realm realm = Realm.getDefaultInstance();
    Number number = realm.where(Empresa.class).max("id");
    return number == null ? 0 : number.intValue() + 1;
}

This method returns the last id of my Company Class ... then every time I want to create a new record, I would call my static method like this ..

        Empresa empresa = realm.createObject(Empresa.class);
        empresa.setId(Empresa.getUltimoId())..
    
answered by 21.10.2016 в 16:37
0

One way to integrate the auto-increase system in realm is to obtain the maximum value of the index and increase +1.

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        User entry = realm.createObject(User.class);

        // increatement indice
        int nextID = (int) realm.where(User.class).max(User.ID).intValue() + 1;

        entry.setId(nextID);
        entry.setName("user1");
        entry.setTeamColor("red");
        entry.setLevel(23);
    }
});

If there is no record entered into the table, it will return error. To solve it:

int nextID = 0;
if (realm.where(User.class).findAll().size() > 0) {
    nextID = (int) realm.where(User.class).max(User.ID).intValue() + 1;
}

It is less optimal, in each new entry it is checked if there are records.

    
answered by 02.09.2016 в 10:35