我需要一些迁移帮助。
我有这个模特:
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
}
新迁移,有2种模型:
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
let category = LinkingObjects(fromType: Category.self,property: "items")
}
class Category: Object {
@objc dynamic var id = NSUUID().uuidString
@objc dynamic var name: String = ""
var items = List<Item>()
override class func primaryKey() -> String {
return "id"
}
}
我们无法为新商品创建新的类别,而最后的商品是孤立的。 我必须创建一个带有“无类别”的新类别,并在其中添加旧项目。
我尝试过,但是没有成功:
private func zeroToOne(_ migration: Migration,_ oldSchemaVersion: UInt64) -> Void {
let category = Category()
category.name = "No Category"
migration.enumerateObjects(ofType: Item.classname()) { oldObject,newObject in
let item = Item()
item.title = newObject?["title"] as! String
item.done = newObject?["done"] as! Bool
category.items.append(item)
}
print("and migration")
}
var configuration: Realm.Configuration {
let config = Realm.Configuration(schemaVersion: 2,migrationBlock: { (migration,oldVersion) in
if oldVersion < 3 {
self.zeroToOne(migration,oldVersion)
}
})
Realm.Configuration.defaultConfiguration = config
return config
}
private func initRealm() {
do {
self.realm = try Realm(configuration: configuration)
} catch let error {
print(error.localizedDescription)
fatalError("Doenst work Realm")
}
}
在此先感谢您的帮助,谢谢:)