0

My insert() function:

public void insert(RSSModel rssModel, String TABLE_NAME) {
    database.getReference()
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child(TABLE_NAME)
            .child(rssModel.getTitle())
            .setValue(rssModel);
    Log.d("alo " + rssModel.getTitle(), "alo");
}

I want to get all RSSModel from TABLE_NAME but it's always return null

public List<RSSModel> getAll(String TABLE_NAME) {
    List<RSSModel> rssModelList = new ArrayList<>();

    DatabaseReference databaseReference = database.getReference(TABLE_NAME);
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
                Log.d("alo " + dataSnapshot.getValue(), "alo");
                rssModelList.add(dataSnapshot.getValue(RSSModel.class));
            }
        }

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

        }
    });

    return rssModelList;
}

Hope that you can help me with this. Thank in advance!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Besides what Frank van Puffelen already answered, please also note that Firebase API is asynchronous. [You cannot return the "rssModelList" as a result of a method](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). – Alex Mamo Apr 07 '21 at 06:32

1 Answers1

0

You're writing to a structure:

/$uid/TABLE_NAME/rssTitle

And then you're reading from: database.getReference(TABLE_NAME)

There is no node /TABLE_NAME, so this read will indeed lead to a non-existing snapshot.

If you want to read the data for a specific user, you will need to specify their UID similar to how you do when writing.

If you want to read the data for all users, you'll need to read from the root (database.getReference()) and then loop over snapshot.getChildren() for the user, and then read the TABLE_NAME (dataSnapshot.child(TABLE_NAME)).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807