4
我目前使用Realm Swift 1.0.1。Do Realm寫入級聯?
假設你有一個境界Object
,有另一種境界Object
作爲一個屬性,像這樣:
import RealmSwift
class Car: Object {
dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
dynamic var model = ""
}
class Garage: Object {
dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
dynamic var carStored: Car?
}
如果然後創建新Car
和Garage
對象,與Car
作爲Garage
的屬性...但只寫新Garage
到Realm
,像這樣......
let teslaCar = Car()
teslaCar.id = 1
teslaCar.model = "Tesla"
let myGarage = Garage()
myGarage.id = 1
myGarage.carStored = teslaCar
let realm = try! Realm()
try! realm.write {
realm.add(myGarage, update: true)
}
...無線將寫入級聯,並且還將teslaCar
保存到Realm
以及myGarage
?
境界雨燕寫文檔:https://realm.io/docs/swift/latest/#writes