Skip to content

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

Open
demolaf wants to merge 2 commits into
masterfrom
fix/database-paging-order-by-child
Open

fix(database): fix DatabasePagingSource pagination when using orderByChild#2336
demolaf wants to merge 2 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 a new package-private DatabasePagingKey type.

@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

public void setUp() throws InterruptedException {
FirebaseApp app;
try {
app = FirebaseApp.getInstance(APP_NAME);

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.

curious if using s separate app is a deliberate choice, if not TestUtils has app init code in TestUtils.getAppInstance()

if (pathIndex != null) {
task = startAtChildValue(key.getChildValue(), key.getNodeKey())
.limitToFirst(params.getLoadSize() + 1).get();
} else {

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.

For non-PathIndex queries this falls back to startAt(null, nodeKey), which works for orderByKey() but likely not for orderByValue() — same cursor issue as #2000. Worth handling here (and in the lastKey construction ~L89) or updating the setQuery() docs to drop orderByValue() until supported.

} else if (childValue instanceof Number) {
return mQuery.startAt(((Number) childValue).doubleValue(), nodeKey);
}
return mQuery.startAt(null, nodeKey);

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.

hmm - looks like this fallback to startAt(null, nodeKey) is the same broken pattern as #2000 for orderByChild() — probably fine for missing child fields, but worth a one-line comment so it's not mistaken as the general case

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