How to set primary key auto increment in realm android

后端 未结 8 574
眼角桃花
眼角桃花 2020-12-14 07:13

I want to set primary key auto increment for my table.

Here is my Class. I have set primary key but I want it to be auto increment primary key.

publ         


        
相关标签:
8条回答
  • 2020-12-14 08:05

    For react native

    ...
    // this is the part where you are saving the newItem
    realm.write(() => {
        // handle the id
        let id: any = realm.objects(this.Schema).max("id");
        newItem.id = id === undefined ? 1 : id++;
        realm.create(this.Schema, newItem);
        resolve(newItem);
    });
    
    0 讨论(0)
  • 2020-12-14 08:08

    Well, @PrimaryKey is only indicates that field is a key. But you set it yourself when creating object and copying it to Realm. Consider using UUID.random(), ot increment it manually. (as in answer from comment): Realm Auto Increament field example

    0 讨论(0)
提交回复
热议问题