How to handle Realm React Native migrations and schemaVersion on iOS?

穿精又带淫゛_ 提交于 2019-12-05 16:58:00

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

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!