objectbox / objectbox/objectbox-java

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

未关闭
#986 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Java
星标
4.6k
派生
311
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 Query.subscribe、DataObserver 和 SubscriptionBuilder 入手,了解为什么 subscription 目前会暴露 List 结果,然后将该行为与 Query.findFirst() 和 Kotlin asFlow 示例进行比较。当提供一种有文档说明的方式来观察单个 nullable T,同时保留现有的多结果观察行为时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
java, kotlin
领域
database
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。