2016-09-21 23 views
0

我試圖訪問存儲在firebase儀表板中的值以在類中的不同函數和方法中使用它們。獲取存儲在firebase中的數據的值

I used this method in this question

我試圖打印他們的價值觀,整個應用程序崩潰,它給了我,他們的零!

他們不是零! 我在viewDidLoad中使用了類似的方法,我可以檢索值到標籤!

let refer = FIRDatabase.database().reference().child("UserDevices") 

var globalEmail : String! 
var globalPhone : String! 
var globalImageUrl: String! 


override func viewWillAppear(_ animated : Bool){ 
    super.viewWillAppear(animated) 
    retrieveUserData{(email,phone,ImageUrl) in 
     self.globalEmail = email 
     self.globalPhone = phone 
     self.globalImageUrl = ImageUrl 
    } 

} 

func retrieveUserData(_ completionBlock : @escaping ((_ email : String?, _ phone : String?, _ ImageUrl: String?)->Void)){ 
    refer.child(byAppendingPath: self.strUserid as String).observe(.value , with: {snapshot in 
     if let userDict = snapshot.value as? [String:AnyObject] { 
      completionBlock(userDict["email"] as! String, userDict["phone"] as! String, userDict["ImageUrl"] as! String) 
     } 
    }) 
} 

var strUserid : NSString! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    print(globalEmail) 
    print(globalImageUrl) 
    print(globalPhone) 

    self.navigationController?.navigationBar.tintColor = UIColor.white 

    print("idis \(self.strUserid)") 

    let ref = FIRDatabase.database().reference().child("UserDevices") 

    self.navigationController?.navigationBar.tintColor = UIColor.white 
    ref.child(byAppendingPath: self.strUserid as String).observe(.value, with: { snapshot in 

     if let dict = snapshot.value as? NSMutableDictionary{ 

      print("dict is \(dict)") 

      if let Provider = dict["name"] as? String 
      { 
       self.DeviceDetailsProvider.text = Provider 
       // self.navigationItem.title = Provider 
      } 
      if let name = dict["DeviceName"] as? String 
      { 
       self.DeviceDetailsName.text = name 
       self.navigationItem.title = name 
      } 
      if let ShortDescription = dict["Description"] as? String 
      { 
       self.DeviceDetailsDescription.text = ShortDescription 
      } 
      if let City = dict["city"] as? String 
      { 
       self.DeviceDetailsCity.text = City 
      } 

     } 
    }) 


    self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl) 

} 

爲什麼我會在這裏發生故障!

+0

哪一行導致崩潰?提到它在哪裏工作,哪裏崩潰! – Santosh

+0

沒有特定的線路。但整個應用程序崩潰 – Mariah

+0

你是什麼意思整個應用程序崩潰? – Santosh

回答

0

變化ref.child(byAppendingPath: self.strUserid as String)

要: -

ref.child(self.strUserid) 

同時刪除let refer = FIRDatabase.database().reference().child("UserDevices")

您不能在範圍外全局初始化您的引用,因爲您不知道您的類正在初始化的順序,可能的情況是您嘗試初始化時尚未初始化您的數據庫let refer

代替referretrieveUserData使用

FIRDatabase.database().reference().child("UserDevices") 

您在的viewController生命週期viewDidLoad中看到的是比viewWillAppear中調用之前:

所以你需要什麼是: -

override func viewDidLoad() { 
super.viewDidLoad() 
    .. 
    retrieveUserData{(email,phone,ImageUrl) in 
    self.globalEmail = email 
    self.globalPhone = phone 
    self.globalImageUrl = ImageUrl 
    self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl) 
    // .. Do your stuff... 

     } 

    } 

enter image description here

閱讀:Viewcontroller's Lifecycle

+0

我已經更新了我的答案。 – Dravidian

+0

答覆已更新...有幫助嗎? – Dravidian

+0

我仍然無法檢索值! – Mariah

相關問題