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)
}