1

I am trying to access nested data values based on profile uid. with this One profile based on uid has multiple friends and friends have location and profile image (in profile) object. I am using CodableFirebase and models for Friend,Profile,LastLocation. for main data list I use observeSingleEvent(of: .value and for all other nested data I need to use .observe(.childAdded .

  • Second thing How would I read all values based on profile uid and save them into an Array that I can then display in a TableView ? I am not getting an idea how to do that please anyone help me.

Firebase Database:

zzV6DQSXUyUkPHgENDbZ9EjXVBj2
         friends
         FTgzbZ9uWBTkiZK9kqLZaAIhEDv1
         conversationUid: "-L_w2yi8gh49GppDP3r5"

         friendStatus: "STATUS_ACCEPTED"

         notify: true

         phoneNumber: "+055441503"

         uid: "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1"

         HHCBYyP4KybcINgZaWVukR967l12
         IcBfQKAnswQ7rivFuZsPvRUcsX43
         LEZ2FUxS1WVaqSEHx5YqnpZ5Els1
         YyFIm4hTorMFq4olpvHJI7e5e3a2
         nkS2cjSvmLUEYEKsAVyErz9HkrJ2
         lastLocation
         batteryStatus: 27

         latitude: 41.0220811

         longitude: 29.0445012

         timeStamp: 1571742057730

         uid: "zzV6DQSXUyUkPHgENDbZ9EjXVBj2"

         profile
         fcmToken: "enMneewiGgg:APA91bHyA4HypWUYhxGTUTTcWch8ZJ_6UUW..."

         name: “My Profile name”

         phoneNumber: "+05588674"

         picture: "profile/zzV6DQSXUyUkPHgENDbZ9EjXVBj2/a995c7f3-7..."

         uid: "zzV6DQSXUyUkPHgENDbZ9EjXVBj2"

Code:

let ref = Database.database().reference()

        ref.child("users").child("zzV6DQSXUyUkPHgENDbZ9EjXVBj2").observeSingleEvent(of: .value, with: { (snapshot) in

            for userSnapshot in snapshot.children.allObjects as! [DataSnapshot] {
                let friendsSnapshot = userSnapshot.childrenCount

                do {
                    let frndList = try FirebaseDecoder().decode(Friend.self, from: friendsSnapshot)
                    self.AppData = [frndList]
                    print(frndList)
                    self.tableView.reloadData()
                } catch let error {
                    print(error)
                }
Newbie
  • 360
  • 3
  • 19
  • That top part in the question which contains your structure is very difficult to read due to formatting. To get your Firebase structure, use the Firebase console->Export JSON and copy and paste a snippet of your structure. I am not really understanding the first part of the question - what child nodes are you trying to access within your structure? The second question *how to read all values* - what values are you trying to read specifically - what do you want to display in your tableView? – Jay Oct 23 '19 at 13:39
  • @Jay First of all thank you. I have just figured it out by myself by reading lots of different firebase realtime database techniques for ios. but getting an issue now on updating values and to reload in tableview. can you please guide me for that ? I just posted my new question. https://stackoverflow.com/questions/58524225/listen-observe-child-change-firebase-and-reload-load-updated-value-in-tableview – Newbie Oct 23 '19 at 13:44
  • @Jay Morning Jay, hope you doing well. First of all thank you again I know I bother you a lot regarding this matter. Posted my new question and I tried my best to write my issue clearly. More so code snippet for swift I don't know to implement that so I shared my project file link. https://stackoverflow.com/questions/58538117/how-to-fetch-nested-data-from-firebase-realtime-database-by-implementing-observe – Newbie Oct 24 '19 at 09:24

0 Answers0