如何从Firebase中检索当前用户的数据 [英] How to retrieve data of current user from firebase

查看:471
本文介绍了如何从Firebase中检索当前用户的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的应用程序来存储用户的图像。用户通过Facebook授权,并与应用程序进行交互。
我坚持从当前用户检索数据。

在我的firebase现在我有这样的结构:



我决定通过uid获取用户数据。
所以我初始化了变量

pre $ let userID = FIRAuth.auth()?currentUser?.uid

然后从数据库中检索一个对象数组

<$ p在

print(snapshot.value as![String:AnyObject])中的observeEventType(.Value,withBlock:{(snapshot) !)

))

}

在我的输出中,我有这个:

 -KJSKE4a2y-okl71vDSx:{
兴趣=男人;
性别=女性;
name = girlsname1;
photo =http://i.imgur.com/VAWlQ0S.gif;
surname = girlsurname1;
},-KJSKE4b8TlvxV-urHQo:{
Interest = men;
性别=女性;
name = girlsname6;
photo =http://media.tumblr.com/tumblr_lnb9aozmM71qbxrlp.gif;
surname = girlsurname6;

等等



没关系,但我的目标是打印当前授权用户的数据。我有一个由uid排序所有对象的想法,但我认为这是不合理的。希望有更简单的解决方案。
$ b 授权用户存储在用户数据库中。个人资料数据库包含我从Facebook获得的用户参数。

解决方案

  let userID: String =(FIRAuth.auth()?。currentUser?.uid)! 
print(当前用户ID是+ userID)

self.dbRef?.child(profile)。child(userID).observeSingleEvent(of:.value,with:{ (快照)
print(snapshot.value)

let userEmail =(snapshot.value as!NSDictionary)[addedByUser] as!String
print(userEmail)


})


I'm, developing a simple app to store users images. User authorized via Facebook, and the interact with app. I stuck with retrieving data from current user.

In my firebase for now I have such structure:

I decided to get user data by uid. So I initialized variable

let userID = FIRAuth.auth()?.currentUser?.uid

Then I retrieve an array of objects from my database

URL_BASE.child("profile").observeEventType(.Value, withBlock: { (snapshot) in

    print (snapshot.value as! [String:AnyObject]!)

})

}

In my output I have this:

 "-KJSKE4a2y-okl71vDSx": {
    Interest = men;
    gender = female;
    name = girlsname1;
    photo = "http://i.imgur.com/VAWlQ0S.gif";
    surname = girlsurname1;
}, "-KJSKE4b8TlvxV-urHQo": {
    Interest = men;
    gender = female;
    name = girlsname6;
    photo = "http://media.tumblr.com/tumblr_lnb9aozmM71qbxrlp.gif";
    surname = girlsurname6;

And so on

It is ok, but my goal is to print data of currently authorized user. I have an idea of sorting all objects by uid, but I think it is irrational. Hope there is simpler solution.

Authorized users stores in users database. profile database consist of user parameters I'm getting from Facebook.

解决方案

    let userID : String = (FIRAuth.auth()?.currentUser?.uid)!
    print("Current user ID is" + userID)

   self.dbRef?.child("profile").child(userID).observeSingleEvent(of: .value, with: {(snapshot) in
        print(snapshot.value)

        let userEmail = (snapshot.value as! NSDictionary)["addedByUser"] as! String
        print(userEmail)


    })

这篇关于如何从Firebase中检索当前用户的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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