0

I'm not good in firebase, but I'm making one project regrading chat app. i have created one not node GroupChat inside that i create group with including members. now i want to check that if userid is available in subchild node like UserInfo/userid . You can get esaily idea by watching below structure . AnyHelp can be appreciate. enter image description here

Here is my coding for retrive data for specific value.

let questionPostsRef = Database.database().reference().child("GroupChat")
let query = questionPostsRef.queryOrdered(byChild: "UserInfo/userid").queryEqual(toValue: FireStoreApp.shared.objUser.strUserID)// Here i pass userid that i want to check in firebase.
 query.observeSingleEvent(of: .childAdded, with: { snapshot in

         print(snapshot.value)
         if !snapshot.exists() { return }
         for child in snapshot.children {
                let childSnap = child as! DataSnapshot
                let dict = childSnap.value as! [String: Any]
                print(dict)
         }
 })

Thank you in advance for your valuable time

deceze
  • 510,633
  • 85
  • 743
  • 889
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49

2 Answers2

0

Try the following:

let questionPostsRef = Database.database().reference().child("GroupChat").child("LfY0_k5TakQ1EuPiGQU").child("UserInfo");
let query = questionPostsRef.queryOrdered(byChild:"userid").queryEqual(toValue: FireStoreApp.shared.objUser.strUserID)// Here i pass userid that i want to check in firebase.
query.observeSingleEvent(of: .value, with: { snapshot in

     print(snapshot.value)
     if !snapshot.exists() { return }
     for child in snapshot.children {
            let childSnap = child as! DataSnapshot
            let dict = childSnap.value as! [String: Any]
            print(dict)
     }
})

You need to traverse the database from top to bottom, therefore you need to add the child child("LfY0_k5TakQ1EuPiGQU") to be able to then check if userId exists or not.

Also when you want to check if a child exists you need to use .value event instead of .childadded.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • but how can i know the value of LfY0_k5TakQ1EuPiGQU this kind of child i have multiple child node right now. – Himanshu Moradiya May 23 '19 at 06:00
  • when adding values to your database, you can add the `childByAutoID` to a variable and then add the variable in the `child()` method same as here https://stackoverflow.com/questions/39328328/firebase-retrieve-childbyautoid-from-realtime-database. The other way is to use firebase authnetication and instead of `childByAutoID` you would use the `userID` – Peter Haddad May 23 '19 at 06:04
  • my group id and Childnode id both are same – Himanshu Moradiya May 23 '19 at 06:20
  • but how can i use directly that value i have only groupchat node inside that i made childnotes that single group chat info inside that i have userinfo subchild node and inside that i have userid and i want to check directly that userid without calling another api request for single node – Himanshu Moradiya May 23 '19 at 06:25
  • and my question is Should i check directly userid from groupchat node ? – Himanshu Moradiya May 23 '19 at 06:27
0

First thing is this code...

.queryOrdered(byChild: "UserInfo/userid")

doesn't work as UserInfo doesn't have a direct child of userid. It's children are 0, 1, 2 etc

Second thing is; in general it's best to try to avoid arrays in NoSQL databases.

A possible solution is to change your structure to match what you are tryng to do.

instead of

GroupChat
   uid
      UserInfo
        0...
        1...
        2...

where 0, 1, 2 are unknowns... change it to

GroupChat
   uid
      UserInfo
        uid_x
           PhotoUrl
           username
        uid_y
           PhotoUrl
           username     
        uid_z
           PhotoUrl
           username

That structure makes it easy to see if a uid already exists in UserInfo as you will know the direct path. It also avoids running a query altogether which saves resources (queries are heavier than direct observers) Here's some pseudo code

let pathToCheck = rootRef.child("GroupChat").child(uid).child("UserInfo").child(uid_to_look_for)

then

pathToCheck.observeSingleEvent.... { snapshot in
  if snapshot.exists() {
     // do something with the snapshot
  } else {
     print("user does not exist in group")
  }
}
Jay
  • 34,438
  • 18
  • 52
  • 81