简体   繁体   English

Android 中的领域迁移问题

[英]Realm Migration Issue in Android

I have a DB which contains few new columns added.我有一个数据库,其中添加了几个新列。 As per my understanding, When someone creates the App for the first time, the default schema version set to 0. Later which we need to increment.根据我的理解,当有人第一次创建应用程序时,默认架构版本设置为0。稍后我们需要增加。

What I have done is, I have set the SCHEMA_VERSION = 1 as there is a new change in the schema and expecting the freshly installed app on any device will contain the default schema as 1.我所做的是,我已经设置了 SCHEMA_VERSION = 1,因为架构中有一个新的变化,并且期望任何设备上新安装的应用程序将包含默认架构为 1。

I am testing with two different devices, one is running Android 6.0 (Marshmellow) another one is Android 8.0 (Oreo).我正在测试两种不同的设备,一种运行 Android 6.0 (Marshmellow),另一种运行 Android 8.0 (Oreo)。

Problem is , when running the app in 6.0, I get the default schema is set as -1 for the first time.问题是,在 6.0 中运行应用程序时,我第一次将默认架构设置为 -1。 Later when I set explicitly Schema Version 1, it sets the default as 1 which is as expected and not calling the Migration as it is installed on the device for the first time.稍后,当我明确设置架构版本 1 时,它将默认值设置为 1,这是预期的,并且不会在第一次安装在设备上时调用迁移。

RealmConfiguration.Builder builder = new RealmConfiguration.Builder();
    builder.schemaVersion(SCHEMA_VERSION); // SCHEMA_VERSION = 1
    builder.migration(new Migration());

Realm.setDefaultConfiguration(builder.build());
Realm.getDefaultInstance().close();

But when I am trying the same code in 8.0, I am getting default schema is set as 0 and when I am setting但是当我在 8.0 中尝试相同的代码时,我将默认架构设置为 0,并且当我设置

builder.schemaVersion(SCHEMA_VERSION); // SCHEMA_VERSION = 1 

,the Migration() is getting called and throwing Field already exists as it is considering SCHEMA_VERSION = 1 is the update. ,Migration() 被调用并且 throwing Field 已经存在,因为它正在考虑SCHEMA_VERSION = 1是更新。

Which means, in 8.0 even if we install the app for the first time, it is taking care that SCHEMA_VERSION = 0 is the default one with all of columns added (Because I can see the db already got the columns added).这意味着,在 8.0 中,即使我们是第一次安装该应用程序,也会注意SCHEMA_VERSION = 0是添加了所有列的默认值(因为我可以看到数据库已经添加了列)。

What could be wrong?可能有什么问题? Why there are two different Default Schema versions (-1 & 0) on fresh App in two devices?为什么在两台设备上的新 App 上有两个不同的默认架构版本(-1 和 0)? And in any case if it is installed on a device for the first time, why would the migration get called?无论如何,如果它是第一次安装在设备上,为什么会调用迁移?

This is the code snippet which I am using before setting the SCHEMA_VERSION to get the Default Schema Versions..这是我在设置SCHEMA_VERSION以获取默认架构版本之前使用的代码片段..

DynamicRealm dynamicRealm = DynamicRealm.getInstance(Realm.getDefaultConfiguration());
long version = dynamicRealm.getVersion();
dynamicRealm.close();

I also have searched SO Q&As, read articles but could not find anything matches my problem.我还搜索了 SO Q&A,阅读了文章,但找不到与我的问题相符的任何内容。 Kindly track the issue here .请在此处跟踪问题。 Would be very grateful if someone could explain what went wrong and how to fix.. Thanks.如果有人能解释出了什么问题以及如何解决,将不胜感激..谢谢。 :) :)

I have figured out, the device Samsung Note 8, started working after restarting the device.我发现,设备 Samsung Note 8 在重新启动设备后开始工作。 Before that, I tried to uninstall and re-install back the application and I am pretty sure I removed everything possible.在此之前,我尝试卸载并重新安装该应用程序,我很确定我删除了所有可能的内容。 Which did not work and started calling the Migration.这不起作用并开始调用迁移。 Now this is not occurring anymore and works perfectly.现在这种情况不再发生并且完美运行。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM