title: How to fetch nested data from Firebase realtime database by implementing observer in swift
body: <p><strong>Firebase data-structure</strong></p>
  
  <ol>
  <li>lastLocations  ( batteryStatus, lat, long, timestamp, uid)</li>
  <li>profiles (name, phoneNumber, picture, uid)</li>
  <li>userFriends ( basis on the uid -> how many friends -> conversationUid, friendStatus, notify, phoneNumber, uid) </li>
  </ol>
  
  <p><strong>My Code:</strong></p>
  
  <ul>
  <li>I have already created tableview and xib for it. </li>
  <li>I have created model for last location, profiles, userFriends. </li>
  <li>I already fetched the friend list but on Observe .ChildAdded</li>
  <li>My uid <code>zzV6DQSXUyUkPHgENDbZ9EjXVBj2</code></li>
  <li>Link: <a href="https://drive.google.com/file/d/19cnkY03MXjrTFgzzPCdvmOuosDRvoMx9/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/19cnkY03MXjrTFgzzPCdvmOuosDRvoMx9/view?usp=sharing</a></li>
  </ul>
  
  <p><strong>Issues:</strong></p>
  
  <ul>
  <li>Not getting an idea how to fetch location &amp; profile with friend list efficient way with observer so any change come it reflects. Firebase is asyncrohonous process. </li>
  <li>observer implementation so data not completely load every-time  </li>
  </ul>
  
  <p><strong>Results to achieve:</strong></p>
  
  <ul>
  <li>I need to show friends list ( name, profile picture, battery status, lat long(address), timeStamp ) on tableview on the basis of my uid.</li>
  </ul>
  
  <p><strong>Firebase JSON</strong> </p>
  
  <pre><code>{
    "lastLocations": {
      "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1": {
        "batteryStatus": 22,
        "latitude": 40.9910537,
        "longitude": 29.020425,
        "timeStamp": 1556568633477,
        "uid": "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1"
      },
      "zzV6DQSXUyUkPHgENDbZ9EjXVBj2": {
        "batteryStatus": 88,
        "latitude": 41.0173995,
        "longitude": 29.1406086,
        "timeStamp": 1571778174360,
        "uid": "zzV6DQSXUyUkPHgENDbZ9EjXVBj2"
      }
    },
    "profiles": {
      "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1": {
        "fcmToken": "fp09-Y9ZAkQ:APA91bFgGB1phr4B9gZScnz7ngpqTb5MchgWRFjHmLCVmWGMJVsyFx0rtrz7roxzpE_MmuSaMc4is-XIu7j718qjRVCSHY4PvbNjL1LZ-iytaeDP0oa8aJgE02wET3cXqKviIRMH",
        "name": "Skander",
        "phoneNumber": "+95644125503",
        "uid": "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1"
      },
      "zzV6DQSXUyUkPHgENDbZ9EjXVBj2": {
        "fcmToken": "enMneewiGgg:APA91bHyA4HypWUYhxGTUTTch8ZJ_6UUWhEIXRokmR-Y-MalwnrtV_zMsJ9p-sU_ZT4pVIvkmtJaCo7LFJYJ9ggfhc1f2HLcN9AoIevEBUqyoMN-HDzkweiUxAbyc84XSQPx7RZ1Xv",
        "name": "Murad",
        "phoneNumber": "+915377588674",
        "picture": "profile/zzV6DQSXUyUkPHgENDbZ9EjXVBj2/a995c7f3-720f-45bf-ac58-b2df934e3dff.jpeg",
        "uid": "zzV6DQSXUyUkPHgENDbZ9EjXVBj2"
      }
    },
    "userFriends": {
      "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1": {
        "zzV6DQSXUyUkPHgENDbZ9EjXVBj2": {
          "conversationUid": "-L_w2yi8gh49GppDP3r5",
          "friendStatus": "STATUS_ACCEPTED",
          "notify": true,
          "phoneNumber": "+915377588674",
          "uid": "zzV6DQSXUyUkPHgENDbZ9EjXVBj2"
        }
      },
      "zzV6DQSXUyUkPHgENDbZ9EjXVBj2": {
        "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1": {
          "conversationUid": "-L_w2yi8gh49GppDP3r5",
          "friendStatus": "STATUS_ACCEPTED",
          "notify": true,
          "phoneNumber": "+915644125503",
          "uid": "FTgzbZ9uWBTkiZK9kqLZaAIhEDv1"
        }
      }
    }
  
  }
  </code></pre>
  
  <p>Swift Function:</p>
  
  <pre><code>func getFrndDataList(){
          AppData.removeAll()
          ref.child("userFriends").child("zzV6DQSXUyUkPHgENDbZ9EjXVBj2").observe(.childAdded, with: { (snapshot) in
  
             guard let data = try? JSONSerialization.data(withJSONObject: snapshot.value as Any) else { return }
             let frndList = try? JSONDecoder().decode(Friend.self, from: data)
  
             self.AppData.append(frndList!)
             self.tableView.reloadData()
             print([frndList])
          })
      }
  </code></pre>