Swift - Firebase observer on .childchanged is not being recognized

↘锁芯ラ 提交于 2021-01-27 16:43:56

问题


Here is the code below, it will not get executed even if I change the child node called "tokens" in the Firebase via app or directly in Firebase!?

handle = ref.child("Users").child(uid).child("tokens").observe(.childChanged, with: { snap in

                print("Changed Token Count: ", snap.value!)

                if snap.value is NSNull {

                    // Child not found

                } else {

                    if (currTokenCount < snap.value as! UInt) {

                        print("Value increased....")

                    } else {

                        print("Value decreased....")

                    }

                }

            }) { (error) in

                print(error.localizedDescription)

            }

回答1:


I assume you have a data structure like this:

Users
  uid1
    tokens: "value of tokens"

If you want to listen for changes to token of a specific user in the above structure, use a .value listener:

handle = ref.child("Users").child(uid).child("tokens").observe(.value, with: { snap in

    print("Changed Token Count: ", snap.value!)

    if snap.value is NSNull {
        // Child not found
    } else {
        if (currTokenCount < snap.value as! UInt) {
            print("Value increased....")
        } else {
            print("Value decreased....")
        }
    }
}) { (error) in
    print(error.localizedDescription)
}

The .child*listeners are only used when you have child nodes undertokens`, like:

Users
  uid1
    tokens
      token1: "value of token1"
      token2: "value of token2"


来源:https://stackoverflow.com/questions/53685511/swift-firebase-observer-on-childchanged-is-not-being-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!