我需要为我的Other Realm
进行迁移。Realm.io RealmSwift其他领域迁移
我通过这种方法得到了我的领域路径(AppDelegate
)。如果用户登录之前,我将检索用户的领域,否则我只会使用Default Realm
。
func getRealmPath() -> String {
let preferences : NSUserDefaults = NSUserDefaults()
let username = preferences.objectForKey(usernameKey) as! String?
if username != nil {
let realmName = ("\(username!).realm")
print("RealmName: \(realmName)", terminator: "")
let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
return documents.stringByAppendingPathComponent(realmName)
}else{
return Realm.Configuration.defaultConfiguration.path!
}
}
我通过这种方法进行了迁移(在AppDelegate:didFinishLaunchingWithOptions
之内调用)。
func updateRealm(){
let config = Realm.Configuration(path: getRealmPath(), schemaVersion: 2, migrationBlock: { (migration, oldSchemaVersion) -> Void in
print("oldSchemaVersion \(oldSchemaVersion)")
migration.create("RLMStringTimestamp", value: ["pKey": NSUUID().UUIDString, "value": "", "updatedAt": NSDate(), "createdAt": NSDate(), "deletedAt": Date().getInitDate(), "updatedBy" : " ", "syncedAt": NSDate() ])
if oldSchemaVersion < 2 {
//MIGRATION
let firstNameTimeStamp = RLMStringTimestamp(newValue: oldObject!["firstName"] as? String)
migration.create("RLMStringTimestamp", value: firstNameTimeStamp)
newObject!["firstName"] = firstNameTimeStamp
}
}
Realm.Configuration.defaultConfiguration = config
//EDIT 2
Realm.Configuration.defaultConfiguration.path = getRealmPath()
//EDIT 1
//<strike>let realm = try! Realm(path: getRealmPath())</strike>
//EDIT 4
print(Realm.Configuration.defaultConfiguration)
//EDIT 3
let realm = try! Realm()
}
For my RLMCustomer Object, i modified
var firstName: String = ""
tovar firstName: RLMStringTimeStamp!
即使我改变
schemaVersion
的东西非常高,
migrationBlock
没有得到调用。任何人都可以帮助我发现我失踪或做错了什么?
运行的应用程序后,它与bad excess, code = 257
编辑崩溃1:
error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Provided schema version 0 is less than last set version 1." UserInfo=0x170660c00 {NSLocalizedDescription=Provided schema version 0 is less than last set version 1.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 50
这似乎是读错的配置文件,我怀疑的错误是由于
Realm.Configuration.defaultConfiguration = config
我该怎么办设置配置为
Other Realm
?
编辑2:
I make my
default realm
to contains the name and path of myother realm
编辑4:
看来这个配置文件是否正确。如果没有来自旧领域的客户记录,我可以毫无问题地运行应用程序。只有在老领域有客户记录时才会崩溃。我可以从oldObject["firstName"]
print(Realm.Configuration.defaultConfiguration)
Realm.Configuration { path = /var/mobile/Containers/Data/Application/8670C084-75E7-4955-89FB-137620C9B00D/Documents/perwyl.realm; inMemoryIdentifier = (null); encryptionKey = (null); readOnly = 0; schemaVersion = 2; migrationBlock = <NSMallocBlock: 0x170451220>; dynamic = 0; customSchema = (null); } oldSchemaVersion 0
,不胜感谢得到的值!
编辑5:解决我的问题
I'm not sure why it crashes if i assign
StringTimestamp object
directly to newObject.
let firstName = (oldObject!["firstName"] as? String)!
let firstNameTimeStamp = StringTimestamp(newValue: firstName)
let testName = migration.create("StringTimestamp",value: firstNameTimeStamp)
newObject!["firstName"] = firstNameTimeStamp //Crashes
newObject!["firstName"] = testName //works
感谢大家的准则! :)
文件打开时,迁移被称为,这只要您检索默认领域发生在你的情况下使用。你能不能展示代码呢? – marius
当您尝试实例化_realm_时会出现什么错误? – Edman
@埃德曼我编辑了我的qn。请看看 – perwyl