realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Add support for `@LinkingObjects`

Open cmelchior opened this issue 4 years ago • 8 comments

Similar to Realm Java, we should add support for mapping inverse relationships. The Java annotation of @LinkingObjects seem fine for this.

class Dog : RealmObject { 
  var name: String = "Fido"
  @LinkingObjects("dog")
  val parents: RealmResult<Person> = RealmResults.empty()
}

class Person : RealmObject {
  var name: String = "Jane"
  var dog: Dog? = null
} 

In Java, the LinkingObjects implementation was highly annoying in Kotlin as it wasn't possible for end users to construct RealmResults themselves which meant that they were forced to do var parents: RealmResults<Person>? = null

We should definitely lift that restriction in the Kotlin SDK. Suggestion would be provide a RealmResults.empty() factory method that basically emulate the empty Results from Core, i.e. it always contains 0 elements. Semantics around queries, adding changelisteners etc. needs to be defined.

cmelchior avatar Nov 23 '21 15:11 cmelchior

Any update on this? We have a database with ~500 relationships we'd like to model. If I can help let me know.

CarsonRedeye avatar Mar 13 '22 04:03 CarsonRedeye

Hi @CarsonRedeye . No update yet...it is on our short-term roadmap to implement, but we don't have any updates right now.

cmelchior avatar Mar 14 '22 09:03 cmelchior

Throwing some API ideas in here, since I know @nhachicha had some ideas as well

a) Similar to Realm Java

@LinkingObjects("child")
val parents: RealmResults<Person> = RealmResults.empty()

The only difference is we need to expose the possibility to create an empty RealmResults. This should be rather easy though with only a few caveats around how to handle RealmResults.query() and RealmResults.version() when calling parents on the unmanaged object

b) Use delegation

val parents: RealmResults<Person> by LinkingObjects("child")

Maybe more Kotlin idiomatic? But I'm not sure we can validate the input at compile time as we do for annotations? This has the advantage we can keep the empty RealmResults as an internal implementation that doesn't have to leak into the public API

Open Questions:

  • [ ] Choose between A or B...or something else
  • [ ] Determine semantics for unmanaged objects. Either throw on all methods or try to mirror a RealmResults as closely as possible?

cmelchior avatar Mar 15 '22 08:03 cmelchior

Still no news about this? We'd like to migrate from java to kotlin but we are missing that

ganfra avatar Jul 21 '22 14:07 ganfra

Throwing some API ideas in here, since I know @nhachicha had some ideas as well

a) Similar to Realm Java

@LinkingObjects("child")
val parents: RealmResults<Person> = RealmResults.empty()

The only difference is we need to expose the possibility to create an empty RealmResults. This should be rather easy though with only a few caveats around how to handle RealmResults.query() and RealmResults.version() when calling parents on the unmanaged object

b) Use delegation

val parents: RealmResults<Person> by LinkingObjects("child")

Maybe more Kotlin idiomatic? But I'm not sure we can validate the input at compile time as we do for annotations? This has the advantage we can keep the empty RealmResults as an internal implementation that doesn't have to leak into the public API

Open Questions:

  • [ ] Choose between A or B...or something else
  • [ ] Determine semantics for unmanaged objects. Either throw on all methods or try to mirror a RealmResults as closely as possible?

@cmelchior

  • I vote the solution with the delegation, it's more a kotlin approach, I don't see how it can cause a problem but we can try anyway. 😅
  • And for the unmanaged objects, the fact to return a realmResult I don't see much interest to mirror a RealmResults as closely as possible since in the end we want to query only managed objects

sirambd avatar Sep 07 '22 13:09 sirambd

Hi @sirambd Thank you for the feedback. We just picked this up, and that is also the direction we are leaning in for the reasons you mentioned.

cmelchior avatar Sep 07 '22 14:09 cmelchior

So, is there a date for it to be released?

ganfra avatar Sep 13 '22 16:09 ganfra

Hi @ganfra We just started the implementation of this, so hopefully we have something to share soon.

cmelchior avatar Sep 16 '22 12:09 cmelchior

@cmelchior Another question, in Realm java you can create a query from a RealmList, I checked the doc and I didn't see how I could do it with Realm kotlin. Is it a choice of yours not to have it here and just have the LinkingObject ?

sirambd avatar Sep 29 '22 12:09 sirambd

@sirambd No, that is an oversight. It will be added.

cmelchior avatar Sep 29 '22 13:09 cmelchior