Ios 删除数据而不是使用迁移 删除数据而不是使用迁移

Ios 删除数据而不是使用迁移 删除数据而不是使用迁移,ios,database,swift3,realm,xcode8,Ios,Database,Swift3,Realm,Xcode8,在我的代码中,我向我的领域类添加了一个属性。有没有一种方法可以删除数据并摆脱我的整个数据库,而不是使用迁移 我仍在测试,这就是我现在可能不需要迁移的原因 请帮帮我,我真的很难使用迁移 谢谢如果您仅在本地测试,则可以删除该应用程序并重新安装。如果您已经在应用商店中安装了应用程序,并且希望为用户准备迁移,那么您可以查看from领域 在AppDelegate上尝试以下操作: import UIKit import RealmSwift // Old data models /* V0 class P

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

我仍在测试,这就是我现在可能不需要迁移的原因

请帮帮我,我真的很难使用迁移


谢谢

如果您仅在本地测试,则可以删除该应用程序并重新安装。如果您已经在应用商店中安装了应用程序,并且希望为用户准备迁移,那么您可以查看from领域

在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
    }
}
导入UIKit
导入RealmSwift
//旧数据模型
/*V0
类人:对象{
动态变量firstName=“”
动态变量lastName=“”
动态变量年龄=0
}
*/
//V1
类人:对象{
动态变量fullName=”“//将firstName和lastName合并到单个字段中
动态变量年龄=0
}
类人:对象{
动态变量fullName=“”
动态变量年龄=0
}
func bundleURL(uName:String)->URL?{
返回Bundle.main.url(forResource:name,扩展名为:“realm”)
}
@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate{
变量窗口:UIWindow?
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:Any]?=nil)->Bool{
window=UIWindow(框架:UIScreen.main.bounds)
window?.rootViewController=UIViewController()
窗口?.makeKeyAndVisible()
//复制旧数据文件以进行迁移
让defaultURL=Realm.Configuration.defaultConfiguration.fileURL!
让defaultParentURL=defaultURL.deletingLastPathComponent()
如果让v0URL=bundleURL(“默认值-v0”){
做{
请尝试FileManager.default.removeItem(位于:defaultURL)
尝试FileManager.default.copyItem(位于:v0URL,收件人:defaultURL)
}捕获{}
}
//定义迁移块
//您可以定义这个内联,但我们将重用它来从多个版本迁移领域文件
//到我们的数据模型的最新版本
让migrationBlock:migrationBlock={migration,oldSchemaVersion在
如果oldschemaversation<1{
enumerateObjects(类型:Person.className()){oldObject,newObject in
如果oldschemaversation<1{
//将名称字段合并到单个字段中
让firstName=oldObject![“firstName”]作为!字符串
让lastName=oldObject![“lastName”]作为!字符串
新对象?[“全名”]=“\(名字)\(姓氏)”
}
}
}
打印(“迁移完成”)
}
Realm.Configuration.defaultConfiguration=Realm.Configuration(schemaVersion:2,migrationBlock:migrationBlock)
//打印出默认领域中所有迁移的对象
//迁移是在域访问上隐式执行的
打印(“默认领域中的迁移对象:\(try!Realm().objects(Person.self))”)
返回真值
}
}

看。

非常感谢,这真的帮助了我