【问题标题】:How to retrieve data from firebase and then update the data using Swift5如何从 firebase 检索数据,然后使用 Swift5 更新数据
【发布时间】:2020-04-16 13:23:46
【问题描述】:

当前用户观看视频广告后,将检索他们的分数(存储在 Firebase 中)并将他们的分数加 1。然后,它将更新文档并显示用户拥有的点数。好像一切都还好,除了,我做不到scoreText.text = point

func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
   let dBRef = Database.database().reference()
    dBRef.child("Users").child(Auth.auth().currentUser!.uid).queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
        guard let dict = snapshot.value as? [String:Any] else {
            print("Error")
            return
        }
        var point = dict["point"] as? Int

        point!+=1
        dBRef.child("users").child(Auth.auth().currentUser!.uid).setValue(["point": point])

        scoreText.text = point

    })

如何显示用户的积分?

[更新代码] 更改代码以访问 Cloud Firestore,而不是实时数据库

 func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
    //[START update_document-increment
    let docRef = db.collection("users").document(UserID!)
    // Atomically increment the population of the city by 1.
    docRef.updateData([
        "points": FieldValue.increment(Int64(1))
    ])
    //[END update_document-increment]


    //insert point value here
    scoreText.text = 

【问题讨论】:

标签: firebase firebase-realtime-database google-cloud-firestore swift5


【解决方案1】:

您显示的代码访问实时数据库,而他的屏幕截图显示 Cloud Firestore。虽然这两个数据库都是 Firebase 的一部分,但它们是完全独立的,一个的 API 不适用于另一个。

要解决此问题,您必须使用 Cloud Firestore API,或将数据输入实时数据库。

【讨论】:

  • 感谢您的澄清,我已经更新了我的代码并更新了我的帖子。现在,每次用户观看广告时,它都会在我的数据库中将“点”增加 1。但是,如何检索该值以便可以在我的 UILabel scoreText.text= 上显示它?
  • 我已经为新问题创建了一个新问题,就像你提到的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多