加入不同的节点,用firebase一次观察它们 [英] Join different nodes to observe them at once with firebase

查看:137
本文介绍了加入不同的节点,用firebase一次观察它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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 ,
用户s:[
user-1,
user-2
]
},
match-2:{
名称: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连接一样)



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

  {
users:{
user-1:{
email:email1,
matches:{
match-1:{
name:Match 1,
users:[
user-1,
user-2

$ b $ match $ 2 $ {
name:Match 2,
users:[
user -1,
user-2,
user-3
]
}
}
}
}





$ b所以我可以一次观察整个结构。

我在iOS上使用了firebase,但是可以随心所欲地用各种语言回复。

谢谢。

解决方案

一个答案,但让我们简化。让我们创建一个用户节点来存储用户和一个匹配节点,以跟踪他们所玩的匹配。然后,我们将执行一个查询来检索与user-1匹配的数据在

$ $ p $ 用户
用户1
名称:某个名字
电子邮件:somename@thing.com
用户2
名称:别名
电子邮件:anothername@thing.com
用户-3
名称:cool name
email:coolname@thing.com

然后匹配节点

 匹配
匹配-1
名称:匹配1
用户
user-1:true
user-2:true
match-2
名称:Match 2
用户
user-1:true
user-2:true
user-3:true
match-3
名称:Match 3
用户
user-2:true
user-3:true

正如您所看到的,我们已经将数据结构化为直接地址我们的查询

  matchesRef.queryOrdered(byChild:users / user-1)。queryEqual(toValue:true)
.observe(.value,其中:{
print(snapshot)
});

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


I'm trying to do a sample application with firebase and I don't have quite understand how I should retrieve nested-flattered data.

Let's suppose I have a db like this

 {
        "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"
                ]
            }
        }
    }

and I want to get all the match of the user-1. What I'm doing now is observe users.user-1.matches to get the matches list and then observe every match so the final flow is:

  • observe users.user-1.matches
  • observe matches.match-1
  • observe matches.match-2
  • ...

The question is: how can I optimize this flow? (like making something like the sql join)

My idea is to get something like this

{
    "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"
                    ]
                }
            }
        }
    }
}

So I can observer the whole structure at once.

I'm using firebase on iOS with swift but feel free to reply in every language you like.

Thanks.

解决方案

The answer linked in the comment is an answer, but let's simplify. Let's create a users node to store the users and a matches node to track the matches they played in.

Then we'll do a query to retrieve which matches user-1 played in:

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"

and then the matches node

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

As you can see, we've structured the data to directly address our query

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

and the snapshot will contain the nodes match-1 and match-2 but not match-3

这篇关于加入不同的节点,用firebase一次观察它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆