【问题标题】:Deleting Data instead of using Migration删除数据而不是使用迁移
【发布时间】:2017-05-16 05:48:26
【问题描述】:

删除数据而不是使用迁移

在我的代码中,我向我的领域类添加了一个属性。有没有办法删除数据并摆脱我的整个数据库而不是使用迁移?

我仍在测试这就是为什么我现在可能不需要迁移。

请帮帮我,我在使用 Migration 时真的很吃力

谢谢

【问题讨论】:

    标签: ios database swift3 realm xcode8


    【解决方案1】:
    【解决方案2】:

    如果您只是在本地进行测试,您可以删除该应用并重新安装。如果您已经在 App Store 上拥有您的应用并希望为用户准备迁移,您可以查看来自 Realm 的Migrations Sample App

    在你的 AppDelegate 上试试这个:

    import UIKit
    import RealmSwift
    
    // Old data models
    /* V0
    class Person: Object {
        dynamic var firstName = ""
        dynamic var lastName = ""
        dynamic var age = 0
    }
    */
    
    // V1
    class Person: Object {
        dynamic var fullName = ""        // combine firstName and lastName into single field
        dynamic var age = 0
    }
    
    
    class Person: Object {
        dynamic var fullName = ""
        dynamic var age = 0
    }
    
    func bundleURL(_ name: String) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: "realm")
    }
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.rootViewController = UIViewController()
            window?.makeKeyAndVisible()
    
            // copy over old data files for migration
            let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
            let defaultParentURL = defaultURL.deletingLastPathComponent()
    
            if let v0URL = bundleURL("default-v0") {
                do {
                    try FileManager.default.removeItem(at: defaultURL)
                    try FileManager.default.copyItem(at: v0URL, to: defaultURL)
                } catch {}
            }
    
            // define a migration block
            // you can define this inline, but we will reuse this to migrate realm files from multiple versions
            // to the most current version of our data model
            let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
                if oldSchemaVersion < 1 {
                    migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
                        if oldSchemaVersion < 1 {
                            // combine name fields into a single field
                            let firstName = oldObject!["firstName"] as! String
                            let lastName = oldObject!["lastName"] as! String
                            newObject?["fullName"] = "\(firstName) \(lastName)"
                        }
                    }
                }
                print("Migration complete.")
            }
    
            Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 2, migrationBlock: migrationBlock)
    
            // print out all migrated objects in the default realm
            // migration is performed implicitly on Realm access
            print("Migrated objects in the default Realm: \(try! Realm().objects(Person.self))")
    
            return true
        }
    }
    

    【讨论】:

    • 非常感谢,这对我很有帮助
    猜你喜欢
    • 2012-03-24
    • 1970-01-01
    • 2015-05-04
    • 2012-07-23
    • 2023-04-05
    • 2020-07-09
    • 1970-01-01
    • 2017-07-24
    • 2020-03-14
    相关资源
    最近更新 更多