Search code examples
javaandroidsqlitemobilerealm

How to set Default Values for fields in Realm?


How can we give a default to the realm model field?

Example :

class Demo extends RealmObject{
    private String id; // I want to set this id to a uuid 
    private boolean isVisibile; // set this to default as true              
}

Solution

  • class Demo extends RealmObject{
        public Demo() {
            id = UUID.randomUUID().toString();
            isVisible = true;
        } 
    
        @PrimaryKey
        private String id; // I want to set this id to a uuid 
        private boolean isVisibile; // set this to default as true              
    }
    

    Should work Realm 2.0+