objectbox / objectbox/objectbox-java

Enable DataObserver to observe T?, not only List<T>

Open
#986 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.6k
Forks
311
PR merge metrics
No merged PRs in 30d

Description

I came across the DataObservers today and noticed, that when you call subscribe on a Query<T>, you will end in a List<T> as result which in my opinion is not what you want, e.g. if you want to find an entity by its id BUT also want to track the changes of this object, you would normally do this: boxStore.boxFor(T::class.java).query {}.findFirst() which would result in a T? result type. Now when i also want to track changes here, i wrap it in a flow (i use kotlin) like this:

@ExperimentalCoroutinesApi
fun <T> Query<T>.asFlow() = callbackFlow {
    val subscription =
        this@asFlow.subscribe()
            .onError { cancel("Database error", it) }
            .observer { data -> offer(data) }
    awaitClose { subscription.cancel() }
}

Suddenly i have to deal with a List<T> due to the fact, that the SubscriptionBuilder returns a SubscriptionBuilder<List<T>>.

Please add a more precise way to differ between single and multiple values. Currently i need to implement workarounds for this, but that should be a standard behaviour

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Query.subscribe, DataObserver, and SubscriptionBuilder to understand why subscriptions currently expose List results, then compare that behavior with Query.findFirst() and the Kotlin asFlow example. Done means providing a documented way to observe a single nullable T while preserving existing multiple-result observation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.