NewCEO
NewCEO

Reputation: 51

how to change from firestore to firebase realtime database

Hi I am converting Firestore to FirebaseDatabase, I am able to do most of the conversion except for this one that I came accross. I am stuck at .getdocuments and Documentchanges. I think .getdocuments is .getchildren? I do not know what the equivalent of Documentchanges is in the realtime firebase. Please help. Thanks you in advance!

here is the code i want to convert

 firestore.collection("Users")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                    if (!queryDocumentSnapshots.getDocuments().isEmpty()) {

                        for (final DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                            if (doc.getType() == DocumentChange.Type.ADDED) {

                                if (!doc.getDocument().getId().equals(mAuth.getCurrentUser().getUid())) {
                                    Friends friends = doc.getDocument().toObject(Friends.class).withId(doc.getDocument().getString("id"));
                                    usersList.add(friends);
                                    usersAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);
                                }

                            }
                        }

                        if(usersList.isEmpty()){
                            refreshLayout.setRefreshing(false);
                            mView.findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }else{
                        refreshLayout.setRefreshing(false);
                        mView.findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                    }

                }
            })

Upvotes: 1

Views: 1005

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317437

There is no direct equivalent in Realtime Database.

The purpose of documentChanges is to know what changes in a set of query results compared to the last time you got results. This only really applies to when you're using a listener to receive realtime updates (which you are not doing here). If you want to know the changes of a DataSnapshot with respect to the prior DataSnapshot you got in a listener, you will have to figure that out for yourself.

But that's kind of beside the point. Your Firestore code here is unnecessarily using document changes to get a list of documents from a single set of results that don't update in real time. If you're not using a realtime listener, then documentChanges actually isn't really the right thing to use. Every document will appear as "ADDED". For a single set of query results, you should just be iterating the list returned by queryDocumentSnapshots.getDocuments().

The equivalent of getDocuments() for a Realtime Database DataSnapshot is just getChildren(), which will give you a way to iterate the child nodes.

Upvotes: 1

Related Questions