Search code examples
androidrealm

Why does Realm create only 8 fields in new RealmObject in some devices?


I use realm-java ver:3.5 in Android studio. I have a JAVA class named Message that extends RealmObject. It has 9 fields with their Getter and Setter.In some mobile devices when I run my app, it'll crash and this error appears in logs:

Field count is less than expected - expected 9 but was 8

I found which field that didn't create, so I changed its name, type and order in class and reran the app but all results were same.

Finally, I added a new field else.Realm only created 8 fields again!!

WHY doesn't Realm create my RealmObject correctly?

public class Message extends RealmObject {
    @PrimaryKey
    private long commandId;

    private Integer id;

    @Required
    private Long initTimeStamp;

    @Required
    private String messageText;

    private User sender;
    private User receiver;

    private MyDateTime sendDate;
    private MyDateTime readDate;

    @Required
    private Integer messageStatus = STATUS_NEW;

public Message() {
    }
/*
 * all setters and getters
 * ...

*/
}

Solution

  • I've added android:allowBackup="true" in manifest file in application section,by default it's true too.So I changed it to android:allowBackup="false" and android:restoreAnyVersion="false" , so my problem solved.

    MORE EXPLANATION
    When we grant allowBackup to our Application,in fact we say to android OS "Hey Android,If I uninstall this app, before that take a backup for it's shared-preference, database and other app data and put them away.
    When we reinstall app, Android replace current data with the old one!