本文介绍了获取检索节点的值并全局使用它们! (Firebase和Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从仪表板的UserProfile节点中获取一些值,并将它们传递到全局变量中,以在observe函数外全局使用它们。

pre > ref.child(UserProfile)。child(FIRAuth.auth()!. currentUser!.uid).observeEventType(.Value,withBlock:{snapshot in $ b $ if let name = snapshot .value!.objectForKey(name)as?String {
print(name)
}
如果让email = snapshot.value!.objectForKey(email)as?String {
print(email)
}
if phone = snapshot.value!.objectForKey(phone)as?String {
print(phone)
}
如果让city = snapshot.value!.objectForKey(city)as?String {
print(city)
}
})

我想将它们传递到观察函数之外,这样我就可以在.swift文件中的任何地方使用它们。由于Firebase函数应该是异步,因此您需要访问这些用户内部的属性。

函数的completionBlock。请注意,一旦调用完成,它们只会提供检索值。

  var globalUserName:String! 
var globalEmail:String!
var globalPhone:String!
var globalCity:String!

重写func viewWillAppear(animated:Bool){
super.viewWillAppear(animated)
retrieveUserData {(name,email,phone,city)in
self.globalUserName =姓名
self.globalEmail =电子邮件
self.globalPhone =电话
self.globalCity =城市



$ b func retrieveUserData(completionBlock:((name:String !, email:String !, phone:String !, city:String!) - > Void)){
ref.child(UserProfile)。child FIRAuth.auth()。currentUser!.uid).observeEventType(.Value,withBlock:{snapshot in
$ b $ if if userDict = snapshot.value as?[String:AnyObject] {

completionBlock(name:userDict [name] as!String,email:userDict [email] as!String,phone:userDict [phone] as!String,city:userDict [city] as!String)
}
})

}


I'm trying to get some values from the UserProfile node from the dashboard and pass them into global variables to use them globally outside the observe function.

ref.child("UserProfile").child(FIRAuth.auth()!.currentUser!.uid).observeEventType(.Value , withBlock: {snapshot in
        if let name =  snapshot.value!.objectForKey("name") as? String {
        print(name)
        }
        if let email =  snapshot.value!.objectForKey("email") as? String {
        print(email)
        }
        if let phone =  snapshot.value!.objectForKey("phone") as? String {
        print(phone)
        }
        if let city =  snapshot.value!.objectForKey("city") as? String {
        print(city)
        }
    })

I want to pass them outside the observe function so I can use them globally anywhere in the .swift file.

解决方案

Since Firebase functions are supposed to be Asynchronous, you need to access these User properties inside the completionBlock of the function. And mind that They will only give retrieved value once the call has been completed.

var globalUserName : String!
var globalEmail : String!
var globalPhone : String!
var globalCity : String!

 override func viewWillAppear(animated : Bool){
     super.viewWillAppear(animated)
        retrieveUserData{(name,email,phone,city) in
            self.globalUserName = name
            self.globalEmail = email
            self.globalPhone = phone
            self.globalCity = city
            }

        }

 func retrieveUserData(completionBlock : ((name : String!,email : String!, phone : String!, city : String!)->Void)){
   ref.child("UserProfile").child(FIRAuth.auth()!.currentUser!.uid).observeEventType(.Value , withBlock: {snapshot in

     if let userDict =  snapshot.value as? [String:AnyObject]  {

          completionBlock(name : userDict["name"] as! String,email : userDict["email"] as! String, phone : userDict["phone"] as! String, city : userDict["city"] as! String)
    }
 })

}

这篇关于获取检索节点的值并全局使用它们! (Firebase和Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:03