skip to Main Content

I want to start off that this issue in NOT impacting my iOS app and strictly in Android using java.

Below is my code:

The first function calls Firebase to get the users in the pool:

private void getPlayerCount() {
    playerInPoolQuery = playerInPoolReference.child(poolIf).orderByValue().equalTo(true);
    readPlayerCountData(playerInPoolQuery, new PicksActivity.AllPicksPlayerCountCallback() {

        @Override
        public void onSuccess(DataSnapshot dataSnapshot) {
            int playerIndex = 0;

            // Determine the number of players in the pool
            numPlayersInPool = (int) dataSnapshot.getChildrenCount();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String userId = snapshot.getKey();
                setPlayerUsername(userId, playerIndex);

                playerIndex = playerIndex + 1;
            }
        }
    });
}

Below is the code for getting the players username:

private void setPlayerUsername(final String userId, final int playerIndex) {
    userQuery = userReference.child(userId).child("username");
    readPlayerUsernameData(userQuery, new SurvivorAllPicksActivity.AllPicksPlayerUsernameCallback() {

        @Override
        public void onSuccess(DataSnapshot dataSnapshot) {
            String username = (String) dataSnapshot.getValue();
            AllPicks player = new AllPicks(username);
            
            playerList.add(player);

            for (int i = 1; i <= currWeek; i++) {
                setPlayerPicks(userId, playerIndex, i);
            }
        }
    });
}

So here is where the issue starts:

In the above code the getPlayerCount function retrieves the Firebase assigned user Ids as example:

0yV3uJZAoDWCZNHuRgxrUlKYFgz1
1q4mOwYUKRgRi3QiC8nLAeVy1T62
425HgyfcpXf1OuA4ndTmry24Kij1
4BCIjoreBuRTNtYIOFZLRKxvk0b2
6TIHOsFuXFQ0v6yBpUn3kVqeHDE3
LNeNupya7GcwsZkd44yV22DNU6f1
M7wxpkSMb4R8rmlvo2A7OyqmLzJ2
UnVFJ49IgTYeNGsV9b0aznhV79B3
bGGtRR18jLe14EcXelgiWbHxi5p2
cU5j1txJyZgwSqsQ72T8pfn30oJ2
lDCzcnZToxMw3iTpMvJ988sJMug2
lvqWVfIpexb7n2DtoPwfVWcYpur2
nnr5ahZHKJfb0vn37eMI5oznyD23
o3Aag8bK29WoU4Aa013dYga3HgB3
q5WNLNjiVrcml4gMbu2ovMU4hyZ2
qnxtPKTVMZRTXurDOvE6aJRhH2o2

The above works exactly as I expected as its the actual order of the players internal ids that are in the pool in alphabetic order (how firebase realtime database internally organizes/lists nodes)

Now here is where I am encountering the issue (which seems to be a race condition).

When the get setPlayerUsername function is called the order in how its executed skews based on the player logged into the app.

What I mean by that, is if the player with the internal identifier bGGtRR18jLe14EcXelgiWbHxi5p2 is logged in they will execute faster than the order of the list provided in the example and that breaks my indexing literally by one, and skews all the players information by one position/index since the logged in player jumps up in order expected.

This is seen happening with any user so if player M7wxpkSMb4R8rmlvo2A7OyqmLzJ2 is logged in then they would be indexed second and skew all the player results.

Literally anyone BUT the two first players in the list above :

0yV3uJZAoDWCZNHuRgxrUlKYFgz1
1q4mOwYUKRgRi3QiC8nLAeVy1T62

When they are logged in and view the players the information is 100% accurate since the indexing is executed in proper order.

Does anyone have any solutions on how I can remedy this issue?

2

Answers


  1. Java’s inheritance mechanism is a fundamental concept that enables the creation of new classes based on existing ones. It allows a class (known as the subclass or child class) to inherit the properties and behaviors (fields, methods) of another class (the superclass or parent class). This promotes code reuse, modularity, and extensibility. Inheritance in Java is single inheritance, meaning each class can directly inherit from only one superclass. However, a class can indirectly inherit from multiple classes through a hierarchy of superclasses, a concept known as multilevel inheritance. Interfaces, another Java feature, provide a form of multiple inheritance for methods, allowing a class to implement multiple interfaces. Through inheritance, Java supports polymorphism, enabling objects of different classes to be treated as instances of a common superclass.

    Blockquote

    Login or Signup to reply.
  2. We’re missing some code in the question, but it indeed is likely a race condition similar to what I answer here a few days ago: Messages are not ordering correctly in Firebase

    To ensure that the players are added to the list in the same order as they are shown in the database, you’ll need to make sure that you add them to that list right away.

    So call playerList.add(player); right away, and then later set the name of that player object when you’ve loaded that information from the database. You’ll need to make sure that the name (and possibly other properties) of the player object are mutable in order to allow thi9s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search