2016-06-22 52 views
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? 
} 

如果然後創建新CarGarage對象,與Car作爲Garage的屬性...但只寫新GarageRealm,像這樣......

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

回答

2

我只是測試它的境界樣本應用程序之一,是絕對肯定的。是的,如果您將對象設置爲另一個Realm對象的鏈接對象,則兩者都將在下一次寫入事務中添加到Realm中。