【问题标题】:Swift: How to migrate custom RealmSwift:如何迁移自定义领域
【发布时间】:2019-06-12 10:16:59
【问题描述】:

我正在尝试对我的自定义领域文件进行迁移。请注意,在这个项目中,我在默认领域文件之上有两个自定义领域文件,一个 Photo.realm 和一个 Transport.realm。我在 Photo 对象中添加了一个新属性,如下所示:

class Photo: Object {
    @objc dynamic var id: String? = nil
    @objc dynamic var secret: String? = nil
    @objc dynamic var server: String? = nil
    @objc dynamic var farm: Int = 0
    @objc dynamic var imageData: Data? = nil
    @objc dynamic var tranport: Transport? //Newly added attribute
}

并且想迁移。我已经阅读了文档,并且由于我正在迁移自定义领域文件,因此我修改了代码并将它们添加到 didFinishLaunchingWithOptions,如下所示:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let defaultConfig = Realm.Configuration()

    if let fileURL = defaultConfig.fileURL {
        let photoRealmFileURL = fileURL.deletingLastPathComponent().appendingPathComponent("Photo.realm")
        let photoMigrationConfig = Realm.Configuration(fileURL: photoRealmFileURL, schemaVersion: 1, migrationBlock: { (migration, oldSchemaVersion) in
            if (oldSchemaVersion < 1) {

            }
        }, objectTypes: [Photo.self])

        do {
            let _ = try Realm(configuration: photoMigrationConfig)
        } catch let error {
            print("Migration Error", error.localizedDescription)
        }

    }

    return true
}

在我的 HomeController 中,我像这样启动我的领域:

class HomeController: UICollectionViewController {

    var photoRealm = try! Realm()
    var transportRealm = try! Realm()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupRealm()
    }

    fileprivate func setupRealm() {
        let defaultConfig = Realm.Configuration()

        if let fileURL = defaultConfig.fileURL {
            let photoRealmFileURL = fileURL.deletingLastPathComponent().appendingPathComponent("Photo.realm")
            let photoConfig = Realm.Configuration(fileURL: photoRealmFileURL, objectTypes: [Photo.self])

            let transportRealmFileURL = fileURL.deletingLastPathComponent().appendingPathComponent("Transport.realm")
            let tranportConfig = Realm.Configuration(fileURL: transportRealmFileURL, objectTypes: [Transport.self])

            do {
                photoRealm = try Realm(configuration: photoConfig)
                transportRealm = try Realm(configuration: tranportConfig)

                let cars = Transport()
                cars.name = "cars"

                let planes = Transport()
                planes.name = "planes"

                try transportRealm.write {
                    transportRealm.add(cars)
                    transportRealm.add(planes)
                }

            } catch let error {
                print("Error setting Realm", error.localizedDescription)
            }
        }
    }

有了这个,我被抛出了错误:

线程 1:致命错误:“尝试!”表达式意外引发错误: Error Domain=io.realm Code=10 "需要迁移,因为 以下错误: - 属性 'Photo.tranport' 已添加。" UserInfo={NSLocalizedDescription=迁移是必需的,因为 以下错误: - 已添加属性“Photo.tranport”。错误代码=10}

我想很明显迁移没有正确完成。由于我正在迁移自定义领域而不是默认领域,因此文档不太清楚如何正确执行此操作。

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

    您可以通过检查要更新的架构版本来迁移您的领域数据库。并将配置传递给默认值 领域的配置。

    将这些代码行添加到您的 appDelegate 的 didFinishLaunchingWithOptions 方法中。在您的领域配置之后并对其进行必要的更改。

    let config = Realm.Configuration(
      // Set the new schema version. This must be greater than the previously used
      // version (if you've never set a schema version before, the version is 0).
      schemaVersion: 1,
    
      // Set the block which will be called automatically when opening a Realm with
      // a schema version lower than the one set above
      migrationBlock: { migration, oldSchemaVersion in
    
        if oldSchemaVersion < 1 {
         // Add your class name where you have added new property 'tranport'.
          migration.enumerate(Photo.className()) { oldObject, newObject in
            newObject?["tranport"] = "Your value"
          }    
        }
      }
    ) 
    Realm.Configuration.defaultConfiguration = config
    

    【讨论】:

    • 即使在迁移自定义领域文件时,我是否也使用 defaultConfiguration?
    • 是的,您需要将自定义配置传递给默认配置。见最后一行代码
    • 我已经尝试过您的代码,它成功迁移,但它使Photo.realm 成为默认领域。我想要实现的是分离,对Photo.realm 的任何修改都会在Photo.realm 上发生。
    • @Koh 太好了!!!请遵循此堆栈答案:stackoverflow.com/a/50462866/3278326
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多