【问题标题】:Checking usertype in Firebase using swift使用 swift 检查 Firebase 中的用户类型
【发布时间】:2016-04-28 21:39:46
【问题描述】:

这里是 swift 和 Firebase 的初学者!我有一个看起来像这样的数据库:

数据库
玩家
ref.authdata.uid-for-player-1
ref.authdata.uid-for-player-2
ref.authdata.uid-for-player-3
教练
ref.authdata.uid-for-coach-1
ref.authdata.uid-for-coach-2
ref.authdata.uid-for-coach-3

我的应用有一半是为球员设计的,另一半是为教练设计的。 如何检查 ref.authData.uid 孩子是否在玩家或教练孩子之下?我的数据结构是否错误?

此外,每个 uid 下都有更多的孩子,我没有在我的图表中包含这些孩子,其中包含有关每个球员/教练的信息。以下是我用于初步构建的身份验证方法。

if ref.authData != nil  {

     print("There is already a user signed in!")
     self.performSegueWithIdentifier("playerLogin", sender: self)

 } else {
     // No user is signed in
 }


感谢您的帮助!

【问题讨论】:

    标签: ios swift authentication firebase


    【解决方案1】:

    tobeisdev 有一个很好的答案(我会接受),链接的答案也可以。

    我也想添加一个替代方案;既然教练和球员都是用户,为什么不采用标准的 Firebase /users 设计模式

    users
      user_id_0
        type: "player"
        name: "Larry"
        team: "team_id_0"
      user_id_1
        type: "player"
        name: "Biff"
        team: "team_id_0"
      user_id_2
        type: "coach"
        name: "Benny"
        team: "team_id_0"
    

    然后你可以添加一个团队节点

    teams
      team_id_0
        team_name: "nerds r' us"
        coach: user_id_2
        players:
           user_id_0: true
           user_id_1: true
    

    这将使您能够查询特定教练或球员并获取他们的 uid 以及了解该球员或教练所属的球队。您还可以组建一支球队并知道教练和球员是谁。

    只是一个想法。

    【讨论】:

    • 这太棒了,这种结构在我以后将要使用这个数据库做的其他事情上效果更好。谢谢杰!在我的最后一个问题中,您提到的关于数据库“越平坦越好”;我开始明白它是如何派上用场的。
    【解决方案2】:

    对于你的第一个问题,我可以和你分享一些在这些父节点中搜索和检查任何 ref-uid 的方法,你可以使用这行代码:

    let REF_BASE = Firebase(url: "https://yourapp.firebaseio.com")
    
    REF_BASE.childByAppendingPath("players").observeEventType(.Value, withBlock: { snapshot in
    
            for snap in snapshot.children {
                let key = snap.key as String
                if key == "ref.authdata.uid-for-player-2" {
                    print(snap.value as String) // to see its value
                }
            }
    
        })
    
    REF_BASE.childByAppendingPath("players").childByAppendingPath("ref.authdata.uid-for-player-2").observeSingleEventOfType(.Value, withBlock: { snapshot in
    
            if snapshot.value as? String == nil {
                print("in usage")
            }
            else {
                print("free")
            }
    
        })
    

    更多详情请查看Jay's answer here

    【讨论】:

    • 啊,好吧,这些查询开始变得有意义了。来自 Parse(R.I.P) 的 firebase 查询的结构对我来说是一个新事物。你能解释一下你的解决方案的最后一部分是做什么的吗?对我来说,它看起来像是检查用户的 UID 是否存在于玩家中,然后它是否执行玩家登录,如果不执行教练登录而不检查相同 UID 的“教练”。我说的对吗?
    • @JoeSloan if snapshot.value is NSNull {if snapshot.value as? String == nil { 如果它在这些块中,您可以检查另一个节点以提供您的 UID 是否存在。但它不像你说的那样在-玩家中。
    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2016-10-15
    • 2016-10-14
    • 2020-09-18
    • 2018-08-15
    相关资源
    最近更新 更多