gpt4 book ai didi

swift - 如何避免在 RealmSwift 中迁移

转载 作者:IT王子 更新时间:2023-10-29 05:22:26 25 4
gpt4 key购买 nike

我只是用 Realm 测试一些配置,因此我在我的 Realm 类中添加和删除了变量和列表。因为我只是在测试,所以我不想经历迁移过程——我也没有任何会影响连续性的数据。

有没有办法绕过 Realm 自动请求的迁移?

最佳答案

无论架构如何变化,有两种方法可以跳过迁移错误。

  1. 使用 deleteRealmIfMigrationNeeded 属性。如果为 true,则在需要迁移时使用提供的架构重新创建 Realm 文件。

    let config = Realm.Configuration(deleteRealmIfMigrationNeeded: true)
    Realm.Configuration.defaultConfiguration = config

    let realm = try! Realm()
    ...

    ‍‍

  2. 每次启动时增加架构版本。 Realm 具有自动迁移功能。如果您不需要迁移现有数据,则可以只增加架构版本。架构将由 Realm 自动更改。

    let config = Realm.Configuration(schemaVersion: try! schemaVersionAtURL(Realm.Configuration.defaultConfiguration.fileURL!) + 1)
    Realm.Configuration.defaultConfiguration = config

    let realm = try! Realm()
    ...

关于swift - 如何避免在 RealmSwift 中迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38193265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com