Skip to content Skip to sidebar Skip to footer

Empty Arraylist - Firebase Data Retrieval

I am currently trying to run a query on my firebase DB, I can see the values that I want in my logs but my method always returns an empty arraylist. Kindly find my code below: publ

Solution 1:

So this is just an attempt to answer your question:

publicstaticvoidgetUserTransactions(String userId, final ArrayList<Transaction> transactionsByUser){
Query userTransactionQuery = mDatabaseReference
        .child("transactions")
        .orderByChild("userId")
        .equalTo(userId);

userTransactionQuery.addValueEventListener(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshottransactionSnapshot: dataSnapshot.getChildren()) {
            // TODO: handle the postTransaction t = transactionSnapshot.getValue(Transaction.class);
            transactionsByUser.add(t);
            Log.w(TAG, t.toString());
        }

        Log.i(TAG, "Collected User Transactions");
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {
        // Getting Transaction failed, log a messageLog.w(TAG, "loadTransactions:onCancelled", databaseError.toException());
        // ...
    }
});
}

Usage

ArrayList<Transaction> transactions =new ArrayList<>();

getUserTransactions("{userId}", transactions);

//check after methodcall while debugging if transactions isempty

If this does not work you going to have to use this event implementation where the ArrayList is being used. Be aware that Firebase queries are asynchronous.

Solution 2:

Use LiveData to solve synchronization problem in asynchronous operation.

privatestaticMutableLiveData<ArrayList<Transaction>> mutableTransactionsByUser = newMutableLiveData<>();

publicstaticMutableLiveData<ArrayList<Transaction>> getUserTransactions(String userId){
    Query userTransactionQuery = mDatabaseReference
            .child("transactions")
            .orderByChild("userId")
            .equalTo(userId);

    final ArrayList<Transaction> transactionsByUser = newArrayList<>();

    userTransactionQuery.addValueEventListener(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshottransactionSnapshot: dataSnapshot.getChildren()) {
                // TODO: handle the postTransaction t = transactionSnapshot.getValue(Transaction.class);
                transactionsByUser.add(t);
                Log.w(TAG, t.toString());
            }

            //Post here
            mutableTransactionsByUser.postValue(transactionsByUser);
            Log.i(TAG, "Collected User Transactions");
        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {
            // Getting Transaction failed, log a messageLog.w(TAG, "loadTransactions:onCancelled", databaseError.toException());
            // ...
        }
    });

    return mutableTransactionsByUser;
}

And then from your Activity/Fragment observe like below:

getUserTransactions(userId).observe(this, newObserver<ArrayList<Transaction>>() {
    @OverridepublicvoidonChanged(ArrayList<Transaction> transactions) {
        // Do your operation here
    }
});

Post a Comment for "Empty Arraylist - Firebase Data Retrieval"