Skip to content

Fix iOS orderByKey() crash with startAt()/endAt() — use 1-param native method when key is null#804

Open
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-ios-orderbykey-crash
Open

Fix iOS orderByKey() crash with startAt()/endAt() — use 1-param native method when key is null#804
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-ios-orderbykey-crash

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

On iOS, orderByKey() combined with startAt()/endAt() crashes with InvalidQueryParameter: You must use queryStartingAtValue: instead of queryStartingAtValue:childKey: when using queryOrderedByKey:. The Apple implementation unconditionally called the 2-parameter ObjC methods even when key was null.

  • startAt(), endAt(), equalTo() (all 3 type overloads each): dispatch to the 1-parameter native method (queryStartingAtValue:, queryEndingAtValue:, queryEqualToValue:) when key is null, and the 2-parameter version (queryStartingAtValue:childKey:, etc.) when key is provided.
// Before — always calls queryStartingAtValue:childKey:, crashes with orderByKey()
Query(ios.queryStartingAtValue(value, key), persistenceEnabled)

// After — dispatches correctly based on key nullability
Query(if (key == null) ios.queryStartingAtValue(value) else ios.queryStartingAtValue(value, key), persistenceEnabled)
  • Integration tests added in commonTest to validate orderByKey() with startAt()/endAt():
    • testOrderByKeyWithStartAtAndEndAt — prefix matching using both startAt() and endAt() (reproduces the original crash scenario)
    • testOrderByKeyWithStartAtorderByKey() with startAt() only
    • testOrderByKeyWithEndAtorderByKey() with endAt() only
Original prompt

This section details on the original issue you should resolve

<issue_title>iOS: orderByKey() with startAt()/endAt() crashes - uses wrong native method</issue_title>
<issue_description>## Bug Description
Using orderByKey() with startAt() or endAt() crashes on iOS with:

InvalidQueryParameter: You must use queryStartingAtValue: instead of 
queryStartingAtValue:childKey: when using queryOrderedByKey:

Reproduction

database.reference("Videos")
    .orderByKey()
    .startAt("prefix_")
    .endAt("prefix_\uf8ff")
    .valueEvents
    .first()

Environment

  • GitLive Firebase Kotlin SDK: 2.4.0
  • Platform: iOS
  • File: firebase-database/src/appleMain/kotlin/dev/gitlive/firebase/database/database.kt

Root Cause

The iOS implementation incorrectly calls queryStartingAtValue:childKey: (2-parameter version)
instead of queryStartingAtValue: (1-parameter version) when using orderByKey().

Workaround

Fetch all data and filter client-side.

Expected Behavior

Should work like Android, using database-side filtering efficiently.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…tive method

When key is null, use the 1-parameter native methods (queryStartingAtValue:,
queryEndingAtValue:, queryEqualToValue:) instead of the 2-parameter versions
(queryStartingAtValue:childKey:, etc.) which crash when used with
queryOrderedByKey: on iOS.

Co-authored-by: Reedyuk <3853006+Reedyuk@users.noreply.github.com>
Copilot AI changed the title [WIP] [BUG-123] Fix iOS orderByKey() crash with startAt()/endAt() Fix iOS orderByKey() crash with startAt()/endAt() — use 1-param native method when key is null Mar 13, 2026
Copilot AI requested a review from Reedyuk March 13, 2026 11:28
@Reedyuk Reedyuk marked this pull request as ready for review March 13, 2026 11:31
@Reedyuk Reedyuk enabled auto-merge (squash) March 13, 2026 13:46
@Reedyuk
Copy link
Copy Markdown
Collaborator

Reedyuk commented Mar 13, 2026

@nbransby - need approval please, "At least 1 approving review is required by reviewers with write access. Approvals from users that collaborated with the coding agent on changes will not satisfy review requirements."

@Reedyuk
Copy link
Copy Markdown
Collaborator

Reedyuk commented Mar 15, 2026

@copilot can you create unit tests too

Tests cover:
- orderByKey() with both startAt() and endAt() (prefix matching)
- orderByKey() with startAt() only
- orderByKey() with endAt() only

Co-authored-by: Reedyuk <3853006+Reedyuk@users.noreply.github.com>
auto-merge was automatically disabled March 15, 2026 14:40

Head branch was pushed to by a user without write access

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 15, 2026

@copilot can you create unit tests too

Added three integration tests in commit 1b412a6:

  • testOrderByKeyWithStartAtAndEndAt — prefix matching using both startAt() and endAt() with orderByKey() (reproduces the original crash scenario)
  • testOrderByKeyWithStartAtorderByKey() with startAt() only
  • testOrderByKeyWithEndAtorderByKey() with endAt() only

Copilot AI requested a review from Reedyuk March 15, 2026 14:40
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.

iOS: orderByKey() with startAt()/endAt() crashes - uses wrong native method

2 participants