Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-558

RealmSingleQuery can emit objects no longer part of the query

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Local
    • None

      RealmSingleQuery might emit objects that no longer fulfill the query conditions. This can happen due to the way we operate on the underlying streams.

      It can only happen if you are querying on properties that might be updated, i.e.

      realm.query<Person>("age < 10").first().asFlow.collect { change: SingleObjectChange<Person> ->
          change.obj!!.age // might be >= 10
      }
      

      The reason is that we flatmapMerge the underlying streams and thus expose the stream on the object found in the RealmResults, but nothing guarantees that this object later full-fill the query condition.

      I.e. this would probably break:

      val person = realm.write { copyToRealm(Person(age = 9)) }
      async {
          realm.query<Person>("age < 10").first().collect {
             println(it.obj?.age)
         }
      }
      realm.write {
        findLatest(person)!!.age = 10
      }
      

      The primary use case of querying primary keys work fine, since the primary key doesn't change:

      realm.query<Person>("id = $0", getId()).first().asFlow()
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: