Realm and auto increment Behavior (Android)

后端 未结 8 2129
野的像风
野的像风 2020-12-14 16:22

I\'m trying to get data from Realm using an ID as a reference. However, when querying for an ID, I\'ve found that Realm is giving me the same ID for all elements (ID of 0).

8条回答
  •  清歌不尽
    2020-12-14 16:43

    Realm currently doesn't support auto incrementing primary keys. However you can easily implement it yourself using something like:

    public int getNextKey() { 
        try { 
             Number number = realm.where(object).max("id");
             if (number != null) {
                 return number.intValue() + 1;
             } else {
                 return 0;
             }
        } catch (ArrayIndexOutOfBoundsException e) { 
             return 0;
        }
    }
    

    I hope that can get you started.

提交回复
热议问题