mohaka
mohaka

Reputation: 23

query in android and Firebase

I have many question about this part. at first this is my code:

private void getUserDetails(UserObject mContact) {
    DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("user");
    Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String  phone = "",
                        name = "";
                for(DataSnapshot childSnapshot : dataSnapshot.getChildren()){
                    if(childSnapshot.child("phone").getValue()!=null)
                        phone = childSnapshot.child("phone").getValue().toString();
                    if(childSnapshot.child("name").getValue()!=null)
                        name = childSnapshot.child("name").getValue().toString();


                    UserObject mUser = new UserObject(childSnapshot.getKey(), name, phone);
                    if (name.equals(phone))
                        for(UserObject mContactIterator : contactList){
                            if(mContactIterator.getPhone().equals(mUser.getPhone())){
                                mUser.setName(mContactIterator.getName());
                            }
                        }

                    userList.add(mUser);
                    mUserListAdapter.notifyDataSetChanged();
                    return;
                }
            }
        }

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

        }
    });
}

my first question: when and why do we use query?

my second question: what does orderByChild method and equalTo method do?

my third question: Is query like data snapShot or something like that and what kind of data it holds in itself?

my 4 question: what does query.addListenerForSingleValueEvent do and when the code inside that run? I know it runs when there is change (for onDataChange method) but I don't what change is it for example is it change in my user list in Firebase or in my realtime database in Firebase.

my 5 question : what does Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone()); do? and what will happen if we doesn't use method equelTo?

my 6 question: what would happen if we use Lod.d("print" , query.toString())

excuse me for having a lot of questions about this but I am a beginner in android and java and I don't how should I solve this problem.

if it is possible for you please answer my question.

thanks for reading my question and sorry if there is any typic problem.

thanks a lot again.

Upvotes: 0

Views: 51

Answers (1)

Swapnil Padaya
Swapnil Padaya

Reputation: 695

Question 1: when and why do we use query?
Ans: Query is the way to retrieve data from the database, so that we can get are data where we have stored it. We use database to keep records for keeping track of usersetc. And a query is nothing but a way to retrieve data from the database. So whenever we want to retrieve something from a database or add some records to it we use query. It's basically the way of talking to database

Question 2: what does orderByChild method and equalTo method do?
Ans: orderByChild is just like sorting data. When we use Comparators or Comparable in java we give how do we want them to get sort and it sorts it according to what we feed in, similarly orderByChild("what you want") this will sort it according to it. equalTo() is use to return nodes with the given key and value you can check the documentation for it too, They are well written.

Question 3: Is query like data snapShot or something like that and what kind of data it holds in itself? Ans: As answered in question 1 Query is a way of retrieving records from the database When we write a query we aim for getting specific records from the database, It may be controversial about query.

Question 5: Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone()) What if we didn't write equelTo
Ans: When we write equalTo(mContact.getPhone()) it will check for specific value in the firebase and return it if found. and if you didn't write equalTo it will just order and the phone number for the following query and then again be back to its normal form. and not return anything. That means when we fire a query it just shows us the data as we want And here orderByChild("phone") and doesn't actually change the look of it in the firebase.

Question 6: Log.d("print" , query.toString()) Ans: we log something to keep track of what is happening in our application, there are various log.i||d||v||w||e
Log.d is for debug
DEBUG: Information interesting for Developers, when trying to debug a problem.

Upvotes: 1

Related Questions