如何在iOS上处理Realm React Native迁移和schemaVersion? [英] How to handle Realm React Native migrations and schemaVersion on iOS?

查看:181
本文介绍了如何在iOS上处理Realm React Native迁移和schemaVersion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我爱上react-native和iOS领域之前,我试图了解有关如何处理迁移的更多信息.这句话令我感到担忧:

Before I fall in love with realm on react-native and iOS, I am trying to learn more about how I can handle migrations. This statement has me concerned:

Realm React Native 0.10.0

迁移目前仅限于更新架构和 如上所述打开Realm时的schemaVersion.资料移转 尚不支持,但将来可能会添加.

Migrations are currently limited to updating the schema and schemaVersion when opening a Realm as outlined above. Data migrations are not yet supported but may be added in the future.

我理解这意味着我每次需要对** any 模式**进行更改时,都需要增加schemaVersion.

I understand this to mean I need to increment schemaVersion each time I make a change to **any schema**.

如何指定多个架构,每个架构都有自己的架构版本?

这不起作用:

export default new Realm(
  {schema: [AppSetting], schemaVersion: 0},
  {schema: [Gps], schemaVersion: 3},
  {schema: [Waypoint], schemaVersion: 4},
  {schema: [FlightPath], schemaVersion: 1},
);

这假设我可能需要经常修改更复杂的架构,直到我做对为止.

This assumes that my more complicated schemas might need to be revised frequently until I get things right.

只要我仅添加新属性,迁移就会简单吗?

Will migrations be simple as long as I only add new properties?

我假设我无法重命名或删除现有属性?

I assume I cannot rename or remove existing properties?

非常感谢领域迁移方面的建议,

Advice on realm migrations is much appreciated,

推荐答案

您需要为整个架构指定一个schemaVersion:

You need to specify a single schemaVersion for your entire schema:

export default new Realm({schema: [AppSetting, Gps, ...], schemaVersion: 0});

当您更新架构中的任何objectSchema时,您需要更改schemaVersion.这意味着某些个体objectSchema在多个schemaVersions中将保持不变,但我认为这要比为每种对象类型使用不同的版本要简单.

When you update any of the objectSchema in your schema you need to bump your schemaVersion. What this means is that some individual objectSchema will remain the same across multiple schemaVersions but I think this is less complicated than having different versions for each object type.

使用新的schemaVersion/schema初始化Realm时,将添加所有新属性,并删除和删除所有属性.因此,如果重命名属性,最终将添加一个具有新名称的新属性,并删除旧属性及其数据.现在,如果要将数据从一个属性复制到另一个属性,则需要分两个步骤进行操作,因此旧属性和新属性都同时存在,您可以进行复制.您还需要跟踪是否进行了复制,因此仅在第一次使用新架构打开领域时才执行此操作.如果仅添加属性,则可以避免大多数(如果不是全部)这种复杂性.

When you initialize a Realm with a new schemaVersion/schema, all new properties are added and missing properties and removed. So if you rename a property this will end up adding a new property with the new name, and removing the old property along with its data. For now if you want to copy data from one property to another you need to do it in two steps, so both the old property and new property exist at the same time allowing you to do the copy. You would also need to keep track of whether you did the copy so you only perform this the first time the the Realm is opened with the new schema. If you are only adding properties you can avoid most if not all of this complexity.

我们没有时间完成初始版本的迁移,但是好消息是大多数功能是在内部实现的,只需要通过js API公开即可.运作方式是什么:打开Realm时,您将能够通过迁移功能,这将使您能够访问迁移前的Realm和迁移后的Realm,并允许您根据需要复制数据.我们希望能在接下来的几个发行版中完成这一工作.

We didn't have time to finish migrations for the initial release, but the good news is most of the functionality is implemented internally and just needs to be exposed through the js apis. How things will work is you will be able to pass a migration function when opening a Realm which will give you access to both the pre and post migrated Realms allowing you to copy data as needed. We hope to have this completed in one of the next few releases.

这篇关于如何在iOS上处理Realm React Native迁移和schemaVersion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆