0

I'm experiencing slightly unusual behaviour when attempting to use computed properties to access linked Objects in a Realm Object subclass.

final class Patient: Object {

    dynamic var name: String = ""

    var parameters = List<Parameter>()

}

final class Parameter: Object {

    dynamic var name: String = ""

    dynamic var patient: Patient? {
        return LinkingObjects(fromType: Patient.self, property: "parameters").first
    }

}

The patient property on the Parameter class returns nil but, if you replace the code with the following, we get the expected behaviour:

var p = LinkingObjects(fromType: Patient.self, property: "parameters")

var q: Patient? {
    return p.first
}

I suspect this is something to do with Realm's internal representation of LinkingObject. The code I used originally was referenced in a previous StackOverflow question and was accepted as a functional solution thus I guess it worked then so perhaps something has changed? Xcode 7, Swift 2.2

Community
  • 1
  • 1
rustproofFish
  • 931
  • 10
  • 32

1 Answers1

0

When Realm added the ability to query inverse relationships, that became the syntax to specify them. See https://realm.io/news/realm-objc-swift-0.100.0/ and https://realm.io/docs/swift/latest/#inverse-relationships for details.

jpsim
  • 14,329
  • 6
  • 51
  • 68