gpt4 book ai didi

swift - 加入不同的节点以使用 firebase 立即观察它们

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:51 24 4
gpt4 key购买 nike

我正在尝试用 firebase 做一个示例应用程序,但我不太明白应该如何检索嵌套的受宠若惊的数据。

假设我有一个这样的数据库

 {
"users": {
"user-1": {
"email": "email1",
"matches": [
"match-1",
"match-2"
]
},
"user-2": {
"email": "email2",
"matches": [
"match-1",
"match-2"
]
},
"user-3": {
"email": "email3",
"matches": [
"match-2"
]
}
},
"matches": {
"match-1": {
"name": "Match 1",
"users": [
"user-1",
"user-2"
]
},
"match-2": {
"name": "Match 2",
"users": [
"user-1",
"user-2",
"user-3"
]
}
}
}

我想获得用户 1 的所有匹配项。我现在正在做的是观察 users.user-1.matches 以获取匹配列表,然后观察每个匹配,因此最终流程是:

  • 观察 users.user-1.matches
  • 观察 matches.match-1
  • 观察 matches.match-2
  • ...

问题是:如何优化这个流程? (比如制作类似 sql join 的东西)

我的想法是得到这样的东西

{
"users": {
"user-1": {
"email": "email1",
"matches": {
"match-1": {
"name": "Match 1",
"users": [
"user-1",
"user-2"
]
},
"match-2": {
"name": "Match 2",
"users": [
"user-1",
"user-2",
"user-3"
]
}
}
}
}
}

所以我可以立即观察整个结构。

我在 iOS 上使用 swift 上的 firebase,但你可以随意用你喜欢的任何语言回复。

谢谢。

最佳答案

评论中链接的答案是一个答案,但让我们简化一下。让我们创建一个用户节点来存储用户和一个比赛节点来跟踪他们参加的比赛。

然后我们将进行查询以检索与 user-1 播放的匹配项:

users
user-1
name: "some name"
email: "somename@thing.com"
user-2
name: "another name"
email: "anothername@thing.com"
user-3
name: "cool name"
email: "coolname@thing.com"

然后是匹配节点

matches
match-1
name: "Match 1"
users
user-1: true
user-2: true
match-2
name: "Match 2"
users
user-1: true
user-2: true
user-3: true
match-3
name: "Match 3"
users
user-2: true
user-3: true

如您所见,我们已将数据结构化以直接解决我们的查询

matchesRef.queryOrdered(byChild: "users/user-1").queryEqual(toValue: true)
.observe(.value, with: { snapshot in
print(snapshot)
});

并且快照将包含节点 match-1 和 match-2 但不包含 match-3

关于swift - 加入不同的节点以使用 firebase 立即观察它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41015445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com