How can I get the last child key from a subset of child keys? For eg. in the loop I want to grab Lqouwhrbcausobc8abc and Lkasjhvbafoshvb8xyz in the getLastChildKey() function
db:
messageIds-userIds
|
@--currentUserId
|
@--XcAUw75y6vXBxCiNl3flrl9qcztob
| |-Laouhfbobhvahsblask : 1
| |-Lwbiuwripibpiwbjvcp : 1
| |-Lqouwhrbcausobc8abc : 1 // I want to grab this messageId
|
@--ZBBglasjdbvj2X8zbMwasdbpBzOOp21w
|-Laofbhaphvbapsjvbpa : 1
|-Lljafhbvpojhfbvaljk : 1
|-Lkasjhvbafoshvb8xyz : 1 // I want to grab this messageId
code:
let ref = Database.database().reference().child(messageIds-userIds)
ref.child(currentUserId)
.queryOrderedByKey()
.queryLimited(toLast: 20)
.observe( .value, with: { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
let userId = child.key
self.getLastChildKey(from: userId, childSnapshot: child)
}
})
func getLastChildKey(from userId: String, childSnapshot: DataSnapshot) {
for child in childSnapshot.children {
let snap = child as! DataSnapshot
}
}