android - 当用户从商店更新 Android 应用程序时,Realm 数据库会发生什么变化?

标签 android google-play realm realm-mobile-platform android-update-app

<分区>

我想知道当用户更新 Android 应用程序时 Realm 会发生什么。 我有一个 Android 应用程序,其中包含一些从 RealmObject 扩展的类。我用它们来保存我的应用程序中的信息。 问题是,当我将新类定义为 RealmObject 并直接从 Android Studio 运行应用程序时,应用程序崩溃了:MyRealmClass 类从该 Realm 的架构中丢失。 我必须卸载以前的版本,然后运行该应用程序才能解决。

当用户从 Play 商店更新应用并且新版本有新的 Realm 类时会发生什么? 应用程序会崩溃吗?如果有,有什么办法可以解决吗?

谢谢!

最佳答案

是的,应用程序会崩溃。您需要添加一个 RealmMigration类。

public class CustomMigration implements RealmMigration {
   @Override
   public long migrate(DynamicRealm realm, long oldVersion, long newVersion) {
     RealmSchema schema = realm.getSchema();

     if (oldVersion == 0) {
       // Migrate from v0 to v1

       schema.create("myNewTable"); // example

       oldVersion++;
     }

     if (oldVersion == 1) {
       // Migrate from v1 to v2
       oldVersion++;
     }

     if (oldVersion < newVersion) {
         throw new IllegalStateException(String.format(Locale.US, "Migration missing from v%d to v%d", oldVersion, newVersion));
     }
   }
 }

RealmConfiguration config = new RealmConfiguration.Builder(context)
    .schemaVersion(2)
    .migration(new MyMigration())
    .build();

Realm.setDefaultConfiguration(config);

// This will automatically trigger the migration if needed
Realm realm = Realm.getDefaultInstance();

关于android - 当用户从商店更新 Android 应用程序时,Realm 数据库会发生什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46060350/

相关文章:

android - Google GeofenceDetection 示例崩溃

android - 自动更新正在运行的应用程序不起作用

objective-c - 带有 NSString 数组的 RLMObject

java - 在测验中的最后一个问题之后转到 Android Studio 中的另一个 Activity

android - 添加 buildFeatures { viewBinding true } 结果为 "Cannot find a setter for <... android:visibility> that accepts parameter type ' int'

Android通过开发者控制台自动更新资源文件

swift - Realm Swift - 如何删除特定索引位置的项目?

java - Realm:如果应用程序及其库之一使用 Realm 并设置(两者)defaultConfiguration,那么 defaultInstance 是什么?

android - 这段代码为什么要用Context呢?

android - 支持的设备数量差异