mk-5 / gdx-fireapp

libGDX Firebase API
Apache License 2.0
63 stars 21 forks source link

An access not only to list values but also to its keys or a wrapper for the getKey() firebase method #16

Closed xvadim closed 5 years ago

xvadim commented 5 years ago

It would be useful to get keys of list items received via the query I am developing a multiplayer game and need such functionality for implementation game rooms. My idea is next:

  1. The first player creates a new record for a game room with at least two children: firstPlayer and secondPlayer. For the firstPlayer the initial value will be the name of creator, for the secondPlayer is an empty string. Also the creator sets a listener for the child secondPlayer
  2. The second player searching the first game record with an empty secondPlayer child and sets his/her name. At the moment I can get the needed record, but can't update it in the firebase database.

readValue should return not just a list of values, but map key=>value

A code will look like:

mGamesRef.orderBy(OrderByMode.ORDER_BY_CHILD, "mSecondPlayer")
    .filter(...)
    .readValue(...)
    .then(new Consumer(...)) {
         @Override
         public void accept(Map values) {
              for(String roomId: values.keySet()) {
                  mGamesRef.inReference(roomId + "/mSecondPlayer").setValue("My name");
                  break;
              }
          }
});

Another more simple, but not so elegant variant is a public method for access of databasePath property of DatabaseDistribution. In this case code for game room creation may look like:

DatabaseDistribution gameEntry = mDatabase.inReference("games").push();
String roomId = gameEntry.getDatabasePath();
gameEntry.setValue(new GameRoom("Creator", "", roomId));

And the second player can read the room id from the received value.

mk-5 commented 5 years ago

Good point but I think it should already works.

I've made a quick test for that and for Andriod platform it works, I'll check that for other platforms soon. Please take a look: https://github.com/mk-5/gdx-fireapp/blob/2.0.0-develop/e2e/core/src/pl/mk5/gdx/fireapp/e2e/tests/DatabaseReadPojoMapWithKeysTest.java

So you should be able to do something like:

.inReference("/game-rooms")
.readValue(Map.class)
.then(new Consumer(...){  public void accept(Map values){} })

it is what you need, isn't it? :) please let me know - because that is a really good point it should be possibility to received database keys

xvadim commented 5 years ago

Thanks for your answer. Unfortunately it works just for readValue, i.e.:

mGamesRef
                .readValue(Map.class)
                .then(new Consumer<Map>() {
                    @Override
                    public void accept(Map map) {
                    }
                });

But for queries:

mGamesRef.orderBy(OrderByMode.ORDER_BY_CHILD, "mSecondPlayer")
                .filter(FilterType.EQUAL_TO, "")
                .filter(FilterType.LIMIT_FIRST, 1)
                .readValue(Map.class)

it crashes:

java.lang.IllegalStateException: Filter should be applied only for the List type at pl.mk5.gdx.fireapp.database.FilteringStateEnsurer.checkFilteringState(

Of course, I can read ALL values and filter its in my code, but it would be nice to have support of filtering out-of-box.

mk-5 commented 5 years ago

yes, that should be possible.

I'll return to you with something soon; please look forward to an answer here :)

mk-5 commented 5 years ago

@xvadim I've just deployed the new 2.0.1 version with Map type allowed for filtering; it should work now, take a look :)

xvadim commented 5 years ago

It works! Thanks a lot!!