-1

I'm working on a small game and I have multiple levels, with different scores for each. I'm trying to make an online record list listing the best scores for each level as uploaded from the devices. for that, (in the OnCreate) I made 10 children to the record child, entered their keys into an array called "keys", and made an array of references called RecRef and an array of my class (called RecordList). then I used a for loop to read the RecordLists and store them. I already tried the exact same thing using a single DatabaseReference and a single RecordList and it worked well. however, the usage of i referring to the RecordLists in the array shows me a syntax error saying j needs to be final. my first question is why is that? but there's a continuation

        for(int i=0;i<=9;i++){
            RecRef[i]=firebaseDatabase.getReference("record").child(keys[i]);
        }
        for(int i=0;i<=9;i++) {
            RecRef[i].addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    //next line is the problem
                    recLists[i] = snapshot.getValue(temp.getClass());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }

so I took their suggestion for how to fix that and added int FinalI=I every iteration. the code runs, but it crushes. I also tried to use a temporary RecordList to store the data:

for(int i=0;i<=9;i++) {
            int finalJ = i;
            RecRef[i].addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    temp = snapshot.getValue(temp.getClass());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
                
            });
            recLists[i]=temp;
        }

now the code does not crash, but all the recLists[i] are the default lists with all their variables set to null. so I made a simple function telling me if a RecordList is the default one, and it tells me that temp is the default one when I try it on OnCreate function but that it is not the default one (but the correct one) when I put it on the OnClick function, which seems weird to me since I do not change temp anywhere else.

it turned out to be a long question, so - why do these different bugs happen? and more importantly - what can I do?

Junaid Khalid
  • 447
  • 4
  • 21
Aviv
  • 13
  • 5
  • If you set breakpoints and run the code in a debugger, or add log statements, you'll see that your `recLists[i]=temp` statement executes before the `temp = snapshot.getValue(temp.getClass())` ever runs. That's because data is loaded from Firebase asynchronously. Any code that needs the data from Firebase has to be inside the `onDataChange`, be called from there, or otherwise synchronized. – Frank van Puffelen May 24 '22 at 12:15

0 Answers0