Sucipto
Sucipto

Reputation: 867

Confusing About Firebase startAt()

I'm trying to make android app with firebase, i want use search feature on firebase using startAt(). But the result make me confused.

I have some firebase data like this.

{
  "siswa" : {
    "100" : {
      "id" : 100,
      "nama" : "Sucipto"
    },
    "101" : {
      "id" : 101,
      "nama" : "Tomo"
    },
    "102" : {
      "id" : 102,
      "nama" : "Fitri"
    },
    "103" : {
      "id" : 103,
      "nama" : "Sumini"
    },
    "104" : {
      "id" : 104,
      "nama" : "Eko"
    },
    "106" : {
      "id" : 106,
      "nama" : "Sulastri"
    }
  }
}

My sample query

// Setup Firebase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
// Firebase siswa ref
final DatabaseReference siswaRef = database.getReference("siswa");
// Query
Query suciptoQuery = siswaRef.orderByChild("nama").startAt("Sucipto");

The result:

 D/Query Sucipto: Nama : Sucipto
 D/Query Sucipto: Nama : Sulastri
 D/Query Sucipto: Nama : Sumini
 D/Query Sucipto: Nama : Tomo

I'm try another test query

Query suciptoQuery = siswaRef.orderByChild("nama").startAt("Su");

Result : Same as above

Another Query :

Query suciptoQuery = siswaRef.orderByChild("nama").startAt("Tomo");

or

Query suciptoQuery = siswaRef.orderByChild("nama").startAt("To");

Result:

D/Query Sucipto: Nama : Tomo

I'm searching about this topic on SO, but still don't have any solution.

Upvotes: 4

Views: 1244

Answers (1)

AL.
AL.

Reputation: 37778

I checked out the startAt() docs and it the logic is quite confusing. But if I do understand it correctly (Yep, not entirely sure, but gonna post ahead, it might help).

In your first try, the starting value is "Sucipto", I think this indicates that succeeding words/strings that are lexicographically greater, are subject included. As you can see in the result, among of the other child that are all in the reference, Eko and Fitri were not returned, since alphabetically, 'S' is far after 'F' and 'E'.

You can verify this trying to add another value, let's say "Sucipta" is included. If you run the query again where startAt()s value is "Sucipto", the return value would still not include "Sucipta" since the last letter of "Sucipta" is higher (or alpahabetically first (a,b,c....) sorry, I just can't find a good term).

I think this might help, you can check out the Firebase Retrieving Data docs for more detailed description. In an example, they also used a letter instead of a number.

Upvotes: 1

Related Questions