RealmSwift:如何正确创建To-One Relationship?

时间:2022-12-20 02:54:32

Let's assume I have:

我们假设我有:

class Dog: Object {
    dynamic var race = ""
    dynamic var name = ""

    override static func primaryKey() -> String? {
        return "race"
    }
}

class Person: Object {
    dynamic var name = ""
    dynamic var address = ""
    dynamic var dog: Dog?

    override static func primaryKey() -> String? {
        return "name"
    }
}

First I create a Dog and save it:

首先,我创建一个狗并保存它:

let dog = Dog()
dog.race = "Dalmatian"

try! realm.write {
    realm.add(dog, update: true)
}

Now I create a Person in a different class. The docs are quite a bit unclear about this scenario. Do I need to save changes for the Dog first before creating the relationship?:

现在我在另一个类中创建一个Person。关于这种情况,文档有点不清楚。在创建关系之前,我是否需要首先保存Dog的更改?:

let person = Person()
person.name = "Jim"

// retrieve dog from realm: 

if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {
    dog.name = "Rex" // Owner gives dog a new name

    // Question:
    // Saving changes to Rex: is this step neccessary?
    try! realm.write {
         realm.add(dog, update: true)
    }

    person.dog = dog
}

try! realm.write {
    realm.add(person, update: true)
}

2 个解决方案

#1


2  

No, and it will cause a crash

不,它会导致崩溃

if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {
    dog.name = "Rex" // Owner gives dog a new name

    person.dog = dog
}

if you want update the dog's name, write like this:

如果你想更新狗的名字,写这样:

if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {

        try! realm.write({ 
            dog.name = "Rex"
        })

        person.dog = dog

    }

see more: Realm.io/updating-objects

看到更多:Realm.io/updating-objects

#2


0  

You can setup a whole object graph as unmanaged objects and persist them all by one call. So you don't need to persist the Dog first and retrieve it again to be able to use it in a relationship.

您可以将整个对象图设置为非托管对象,并通过一次调用将它们全部保留。因此,您不需要先保留Dog并再次检索它以便能够在关系中使用它。

let dog = Dog()
dog.race = "Dalmatian"

let person = Person()
person.name = "Jim"
person.dog = dog

try! realm.write {
    realm.add(person, update: true)
}

#1


2  

No, and it will cause a crash

不,它会导致崩溃

if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {
    dog.name = "Rex" // Owner gives dog a new name

    person.dog = dog
}

if you want update the dog's name, write like this:

如果你想更新狗的名字,写这样:

if let dog = realm.objectForPrimaryKey(Dog.self, key: "Dalmatian") {

        try! realm.write({ 
            dog.name = "Rex"
        })

        person.dog = dog

    }

see more: Realm.io/updating-objects

看到更多:Realm.io/updating-objects

#2


0  

You can setup a whole object graph as unmanaged objects and persist them all by one call. So you don't need to persist the Dog first and retrieve it again to be able to use it in a relationship.

您可以将整个对象图设置为非托管对象,并通过一次调用将它们全部保留。因此,您不需要先保留Dog并再次检索它以便能够在关系中使用它。

let dog = Dog()
dog.race = "Dalmatian"

let person = Person()
person.name = "Jim"
person.dog = dog

try! realm.write {
    realm.add(person, update: true)
}