[英]Swift -Firebase get last childKey from a subset of snapshot.children
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在循环中,我想在
getLastChildKey()
函数中获取Lqouwhrbcausobc8abc
和Lkasjhvbafoshvb8xyz
db: D b:
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
}
}
There is no way to get just the last child node from each child node with a single read operation.无法通过单个读取操作从每个子节点中仅获取最后一个子节点。 While it is possible to get the last child node of a node, you will have to know the complete path of the parent node in that case.
虽然可以获得节点的最后一个子节点,但在这种情况下,您必须知道父节点的完整路径。
This boils down to the fact that Firebase Realtime Database queries operate on a list of child nodes, and not on a tree.这归结为 Firebase 实时数据库查询对子节点列表而不是树进行操作的事实。
So you can request only the last (or first) child of messageIds-userIds
, since you know the full path to that messageIds-userIds
node.因此,您只能请求
messageIds-userIds
的最后一个(或第一个)子节点,因为您知道该messageIds-userIds
节点的完整路径。
If you know the currentUserId
then you can request the first/last child of that node too.如果您知道
currentUserId
那么您也可以请求该节点的第一个/最后一个子节点。
And if you were to know both the currentUserId
and XcAUw75y6vXBxCiNl3flrl9qcztob
, then you could request the first/last child of that node too.如果您知道
currentUserId
和XcAUw75y6vXBxCiNl3flrl9qcztob
,那么您也可以请求该节点的第一个/最后一个子节点。
But if you only know the messageIds-userIds
and want to get the last child of each of its currentUserId/$XcAUw75y6vXBxCiNl3flrl9qcztob
child nodes, you will need to read the entire structure as you're doing now.但是,如果您只知道
messageIds-userIds
并且想要获取其每个currentUserId/$XcAUw75y6vXBxCiNl3flrl9qcztob
子节点的最后一个子节点,则需要像现在一样阅读整个结构。
It often helps to model the data for the use-cases of your app.它通常有助于为您的应用程序的用例建模数据。 In this case, consider storing the last child node for each path in a single list.
在这种情况下,考虑将每个路径的最后一个子节点存储在单个列表中。
lastChildNodes: {
"currentUserId XcAUw75y6vXBxCiNl3flrl9qcztob": "-Lqouwhrbcausobc8abc"
"currentUserId ZBBglasjdbvj2X8zbMwasdbpBzOOp21w": "-Lkasjhvbafoshvb8xyz"
}
Code sample:代码示例:
func getLastChildKey(from userId: String, childSnapshot: DataSnapshot) {
DataSnapshot last = nil
for child in childSnapshot.children {
snap = child as! DataSnapshot
}
return snap
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.