Firebase removeObserver问题迅速

时间:2018-04-27 05:00:09

标签: ios swift firebase firebase-realtime-database

我的应用程序使用Firebase。 我目前面临从DatabaseReference中删除观察者的问题。 在删除任何值时,它会删除所有相关(多个条目)值,但是当它不删除该引用的观察者时。 当我下次尝试输入相同的条目时,它会自动删除 并且RemoveAllObservers()也无效。

   func deleteNodeForGatewayFromFirebase(gID:String,ownerID:String)
   {
    ref.observe(.value, with: { snapshot in
        var loopCount = 1  //  count loops to see how may time trying to loop
        for child in snapshot.children
        {
            print("            ")
            print("            ")
            print("child node ------ loop \(loopCount)")

            let snap = child as! DataSnapshot //each child is a snapshot
            let dict = snap.value as! [String: Any] // the value is a dictionary

            let gId = dict["gatewayId"]
            let nId=dict["nodeId"]
            let owner = dict["ownerId"]


            print("the bits ------")
            print("gatewayId .... \(String(describing: gId))")
            print("nodeId .... \(String(describing: nId))")
            print("ownerId .... \(String(describing: owner))")
            loopCount += 1

            if gID.elementsEqual(gId as! String)
            {
                print("owner found")
                self.ref.child(nId as! String).removeValue { (error, ref) in
                    if error != nil
                    {
                        print("error.... \(String(describing: error))")
                    }
                    else
                    {
                        ref.removeValue()
                        print("entry deleted.....")
                    }
                }
                return
            }else
            {
                print("no gateway found")
            }
        }
    })
}

1 个答案:

答案 0 :(得分:2)

虽然显示的代码存在缺陷,但它会立即覆盖beaconRegiondataDictionary的值,即使这些问题已修复,您仍然无法同时在iOS上传输两个信标。

iOS根本不允许这样做。应用程序一次只能发送一个广告。但是,您可以通过在两者之间快速交替来模拟传输两个信标。有关详细信息,请参阅我的相关答案:https://stackoverflow.com/a/21553009/1461050