领域是否需要明确添加嵌套对象
Does realm need nested objects to be added explicitly
考虑两个模型:
class Person: Object {
@objc dynamic var name: String
@objc dynamic var pet: Animal
}
class Animal: Object {
@objc dynamic var name: String
}
现在,如果我必须创建并添加一个带宠物的新人。以下是否足够?
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person) // <----- This
}
或者我也需要显式添加 dog(Nested Object)?
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person)
realm.add(dog) // <----- Like This
}
够了
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person) // <----- This
}
Animal 对象将自动插入,只需对该模型进行查询即可得到它
考虑两个模型:
class Person: Object {
@objc dynamic var name: String
@objc dynamic var pet: Animal
}
class Animal: Object {
@objc dynamic var name: String
}
现在,如果我必须创建并添加一个带宠物的新人。以下是否足够?
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person) // <----- This
}
或者我也需要显式添加 dog(Nested Object)?
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person)
realm.add(dog) // <----- Like This
}
够了
realm.write{
let dog = Animal(name: "Daisy")
let person = Person(name: "John Wick", pet: dog)
realm.add(person) // <----- This
}
Animal 对象将自动插入,只需对该模型进行查询即可得到它