Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift firebase一对多示例?_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

swift firebase一对多示例?

swift firebase一对多示例?,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我已经创建了以下两个表。我想选择tablefriends,我想显示table user中的姓名?任何人都可以帮助我,例如在swift请谢谢你 表用户 id name email 餐桌朋友 id user_id friend_id 您的实时数据库应该是这样的 { "users": { "1": { "name": "John Doe", "email": "john@example.com" }

我已经创建了以下两个表。我想选择tablefriends,我想显示table user中的姓名?任何人都可以帮助我,例如在swift请谢谢你

表用户

 id
 name
 email
餐桌朋友

 id
 user_id
 friend_id

您的实时数据库应该是这样的

{
    "users": {
        "1": {
            "name": "John Doe",
            "email": "john@example.com"
        },
        "2": {
            "name": "Jane Doe",
            "email": "jane@example.com"
        }
    },
    "user-friends": {
        "1": {
            "2": true
        },
        "2": {
            "1": true
        }
    }
}
为了获得John的朋友,我们观察路径
/user friends/1

let uid = 1 // the uid of the user we want the friends of
let ref = FIRDatabase.database().reference().child('user-friends/\(uid)')

ref.observeEventType(.ChildAdded, block: { snapshot in

    let uid = snapshot.key // the uid of a user who is John's friend
    let ref = FIRDatabase.database().reference().child('users/\(uid)')

    ref.observeSingleEventOfType(.Value, block: { snapshot in
        if let json = snapshot.value as? [String:AnyObject] {
            print(json["name"])
        }
    })
})
欢迎来到stack overflow:-)“请给我代码”不是这个网站的工作方式。展示你自己的努力并解释你的问题。请看