rustproofFish
rustproofFish

Reputation: 999

Computed properties using Realm LinkedObject instances return nil

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

Upvotes: 0

Views: 329

Answers (1)

jpsim
jpsim

Reputation: 14409

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.

Upvotes: 0

Related Questions