Realm-java: Realm data wiped out after update app from Play Store

Created on 10 Jan 2019  路  5Comments  路  Source: realm/realm-java

Everytime user update from play store, their data wiped out. I try to reproduce the problem by installing the previous version (2.1) through my pc, then update (2.2) at Play Store, but couldn't reproduce.

Can help me to figure out the real problem. Thanks :)

My setup as below:-

Android Studio Version : 3.1.2
Realm-gradle-plugin:4.3.0
TargetSdkVersion 28
CompileSdkVersion 28
BuildToolsVersion "28.0.3"

Realm.init(context);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);

O-Community T-Help

All 5 comments

You have set .deleteRealmIfMigrationNeeded(), so if people are upgrading from a version that is causing a migration, their Realm will be wiped. Generally, that setting should only be used during development.

deleteRealmIfMigrationNeeded() will delete your Realm if there is a new RealmObject class, or a RealmObject class is removed (I think), or a new field is added to a RealmObject class, or a field is removed from a RealmObject class, or you add a new field attribute to a field of a RealmObject class, or you remove a field attribute of a RealmObject class (@Index, @Required...)

This is expected behavior. If you don't want to lose data, write a migration. Please note that the schema must match perfectly between the two schema versions or you'll get a RealmMigrationNeededException.

Migrations are not triggered if you remove classes or properties from the schema again as the file is allowed to contain "extra" tables and properties, but otherwise 鈽濓笍 is correct.

(and generally, you probably want to actually remove classes/fields no longer used as part of the migration anyway to avoid confusion down the line).

Oh, that's new behavior that came in with the object store changes to "better suit sync", then.

I guess the part that was "I think" is outdated :smile:

I assume the above answered your question. If not feel free to re-open. Closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pawlo2102 picture pawlo2102  路  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  路  3Comments

mithrann picture mithrann  路  3Comments

bryanspano picture bryanspano  路  3Comments

David-Kuper picture David-Kuper  路  3Comments