Skip to content

fix(database): fix DatabasePagingSource pagination when using orderByChild#2336

Merged
demolaf merged 4 commits into
masterfrom
fix/database-paging-order-by-child
Jun 19, 2026
Merged

fix(database): fix DatabasePagingSource pagination when using orderByChild#2336
demolaf merged 4 commits into
masterfrom
fix/database-paging-order-by-child

Conversation

@demolaf

@demolaf demolaf commented Jun 17, 2026

Copy link
Copy Markdown
Member

Fixes #2000.

DatabasePagingSource was calling startAt(null, key) for all queries, but Firebase Database ignores the key parameter when the query uses orderByChild(), causing every subsequent page load to return the same first page of results and throwing IllegalStateException: The same value was passed as the nextKey in two sequential Pages.

The fix detects orderByChild() queries via the internal PathIndex and calls startAt(childValue, key) instead, storing both the child value and node key as the page cursor in the new DatabasePagingKey type.

Changes:

  • DatabasePagingKey — made public (required for cross-package Kotlin access) with equals()/hashCode() (required by the Paging library for key diffing and state restoration)
  • DatabasePagingSource — extends cursor fix to orderByValue() queries via ValueIndex in addition to orderByChild() via PathIndex; surfaces load errors to logcat via onErrorReturn
  • FirebaseRecyclerPagingAdapter — replaces deprecated @OnLifecycleEvent with LifecycleEventObserver (removed in lifecycle 2.7.0)
  • Adds instrumented test verifying no duplicate keys across pages when using orderByChild()

@demolaf demolaf marked this pull request as draft June 17, 2026 15:47
@demolaf demolaf marked this pull request as ready for review June 17, 2026 15:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new DatabasePagingKey class to replace String as the paging key in DatabasePagingSource and DatabasePagingOptions, allowing the paging source to correctly handle queries ordered by child values without producing duplicate items across pages. It also adds integration tests to verify this behavior. The review feedback suggests making DatabasePagingKey public and implementing equals() and hashCode() to ensure compatibility with the Paging library, as well as simplifying numeric type handling in startAtChildValue by checking for Number instead of Double and Long separately.

Comment thread database/src/main/java/com/firebase/ui/database/paging/DatabasePagingKey.java Outdated
@demolaf demolaf requested a review from just1and0 June 18, 2026 14:54

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a perfect fix for the existing issue but may have a not-too-hard extension possibility to be even more general and fix the general case that 2000 was just a specific instance of

@demolaf demolaf merged commit 51512a5 into master Jun 19, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in studio-2394994192-60a69 Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DatabasePagingSource not working when firebase query contains orderByChild clause.

2 participants