Swift 更改realmexamples中的所有者数量

Swift 更改realmexamples中的所有者数量,swift,realm,owner,Swift,Realm,Owner,我已经下载了Realm并打开了RealmExamples应用程序 RealmExamples应用程序包含在从 正在尝试添加所有者以获取打印输出以提供多于1个。还增加了打印时间 println("\(dog.name) has \(ownerNames.count) owners (\(ownerNames)) is \(dog.age) years old") 我添加了一只狗和一个主人,给了一只狗两个主人,但打印出来的只有一个主人两次 import UIKit import Realm

我已经下载了Realm并打开了RealmExamples应用程序

RealmExamples应用程序包含在从

正在尝试添加所有者以获取打印输出以提供多于1个。还增加了打印时间

 println("\(dog.name) has \(ownerNames.count) owners (\(ownerNames)) is \(dog.age) years old")
我添加了一只狗和一个主人,给了一只狗两个主人,但打印出来的只有一个主人两次

  import UIKit
import Realm

class Dog: RLMObject {
    dynamic var name = ""
    dynamic var age = 0
    var owners: [Person] {
        // Realm doesn't persist this property because it only has a getter defined
        // Define "owners" as the inverse relationship to Person.dogs
        return linkingObjectsOfClass("Person", forProperty: "dogs") as [Person]
    }
}

class Person: RLMObject {
    dynamic var name = ""
    dynamic var dogs = RLMArray(objectClassName: Dog.className())
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window!.rootViewController = UIViewController()
    self.window!.makeKeyAndVisible()

    NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)

    let realm = RLMRealm.defaultRealm()
    realm.transactionWithBlock {
        Person.createInRealm(realm, withObject: ["John", [["Fido", 1]]])
        Person.createInRealm(realm, withObject: ["colin", [["Fido", 1]]])
      Person.createInRealm(realm, withObject: ["colin", [["simba", 3]]])
    }

    // Log all dogs and their owners using the "owners" inverse relationship
    let allDogs = Dog.allObjects()
    for dog in allDogs {
        let dog = dog as Dog
        let ownerNames = dog.owners.map { $0.name }
        println("\(dog.name) has \(ownerNames.count) owners \(ownerNames) is \(dog.age) years old")
    }
    return true
}
}

println给出了

Fido has 1 owners ([John]) is 1 years old
Fido has 1 owners ([colin]) is 1 years old
simba has 1 owners ([colin]) is 3 years old

我该怎么做才能让Fido有两个所有者。

你几乎就得到了它。在下面的代码中,您正在创建3个新的人和3只新的狗

realm.transactionWithBlock {
    Person.createInRealm(realm, withObject: ["John", [["Fido", 1]]])
    Person.createInRealm(realm, withObject: ["colin", [["Fido", 1]]])
  Person.createInRealm(realm, withObject: ["colin", [["simba", 3]]])
}
我们来分析一下

Person.createInRealm(realm, withObject: ["John", [["Fido", 1]]])
是否等同于这样做:

//Creating our first person and dog relationship
let firstDog = Dog()
firstDog.name = "Fido"
firstDog.age = 1

let person1 = Person()
person1.name = "John"
person1.dogs = [firstDog]
//Creating our second person and dog relationship
let secondDog = Dog()
secondDog.name = "Fido"
secondDog.age = 1

let person2 = Person()
person2.name = "colin"
person2.dogs = [secondDog]
现在,我们来看看下一个人和狗的关系正在创建

//Your original code
Person.createInRealm(realm, withObject: ["colin", [["Fido", 1]]])
是否等同于这样做:

//Creating our first person and dog relationship
let firstDog = Dog()
firstDog.name = "Fido"
firstDog.age = 1

let person1 = Person()
person1.name = "John"
person1.dogs = [firstDog]
//Creating our second person and dog relationship
let secondDog = Dog()
secondDog.name = "Fido"
secondDog.age = 1

let person2 = Person()
person2.name = "colin"
person2.dogs = [secondDog]
因此,即使这些狗有相同的名字和年龄,它们也不是同一只狗。第一条狗!=第二条狗。我认为最简单的方法是写出与我上面所做的类似的长表单,然后这样做

person2.dogs = [firstDog]
现在,它指的是添加到第一个人的同一个dog对象

替代解决方案:


另一种方法是使用primaryKey,这样每只狗都是唯一的,您可以在将它们添加到另一个人之前通过primaryKey获取它们。该解决方案涉及面稍大一些,但通常是执行此类操作的正确方法,因为它们变得越来越复杂。

嘿,cpmac,你能分享更多关于如何设置模型以及如何查询数据库中的所有者名称的代码吗?谢谢你的回复。我已经添加了appdelagate文件的完整代码,谢谢你的回复。这段代码似乎暗示狗和主人之间的唯一联系是名字。var owners:[Person]{//Realm没有持久化此属性,因为它只定义了一个getter//Define owners作为Person的反向关系。dogs返回linkingObjectsOfClassPerson,forProperty:dogs作为[Person]然后dynamic var dogs=rlmarayobjectclassname:Dog.classNameNp!属性dogs是链接,因为您在Person对象上有一个存储关系dynamic var dogs.Dog.className=Dog。您可能会将其误认为Dog.className=Dog上的属性名称,即Dog.name,这是不正确的。这会与Dog.name Person混淆.name.我将最后一个交易行更改为[Colin,[[Rex,3][Fido,1]],它打印了第四行-Rex有一个所有者[Colin].正确,每次你打电话给Person.createInRealm…你实际上是在创建全新的Person对象和dog对象,即使它们具有相同的名称。维护关系的最佳方法是完整地写出你的代码,类似于我上面的答案。这非常清楚地表明哪只狗和哪只人一起。你也可以告诉我们e主键,这将迫使你只有一个人有一个特定的ID。如果这没有意义,请加入下周周二的实时办公时间!