来自Firebase的数据不会显示在Swift 3应用程序中

时间:2017-11-22 12:32:57

标签: ios swift firebase firebase-realtime-database

我无法从firebase检索数据并将其显示在标签上,请帮助我。

Firebase数据:

authentication-f5ddeaddclose 
 user_clicks
 X0KFbJAlrGUla9m1XebyhX0YZxB3
 Counts: 3
 nmsvVPmJWAbqPr0U4WbfHiqFMkE2
 Counts: 13

Firebase规则:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Xcode中:

var ref:DatabaseReference?
var refHandle: UInt!




@IBAction func save(_ sender: Any)
{

    let user_counts: Int = counts

    ref?.child("user_clicks").child(userID).setValue(["Counts":user_counts])
}


override func viewDidLoad() {
    super.viewDidLoad()

    ref = Database.database().reference()

    refHandle = ref?.observe(DataEventType.value, with: { (snapshot) in
        let postDict = snapshot.value as? [String : AnyObject] ?? [:]

        print (postDict)
    })

    ref?.child("user_clicks").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary



        let user_counts = value?["Counts"] as? String ?? ""

        self.label_usercounts.text = user_counts

        // ...
    }) { (error) in
        print(error.localizedDescription)
    }

}

当我打开应用程序时,标签不显示任何内容,只是文本消失。我不知道该怎么做,你能帮帮我吗?

0 个答案:

没有答案