ios - 观察者使用 removeAllObervers 多次触发

标签 ios swift firebase firebase-realtime-database

我有一个 IBAction,当按下它时,它会创建一个观察者监听器并获取快照数据。

当某个子节点的值发生变化时,我想关闭当前的 ViewController 并呈现一个不同的 ViewController。

当 ViewController 被关闭时,我试图删除所有观察者句柄。

但是当我构建并运行时,从我的控制台输出来看,我的 Firebase Observer 似乎被多次调用,原因不明。

This is being executed in the observer
This is being executed in the observer
This is being executed in the observer
This is being executed in the observer
This is being executed in the observer
This is being executed in the observer
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to present <SSM.activeViewController: 0x13eb3b200> on <SSM.userProfileViewController: 0x13f18c400> while a presentation is in progress!
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to dismiss from view controller <UINavigationController: 0x13f24ba00> while a presentation or dismiss is in progress!
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to present <SSM.activeViewController: 0x13eb3b200> on <SSM.userProfileViewController: 0x13f18c400> while a presentation is in progress!
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to dismiss from view controller <UINavigationController: 0x13f24ba00> while a presentation or dismiss is in progress!
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to present <SSM.activeViewController: 0x13eb3b200> on <SSM.userProfileViewController: 0x13f18c400> while a presentation is in progress!
2016-07-25 14:03:06.125 SSM[636:140771] Warning: Attempt to dismiss from view controller <UINavigationController: 0x13f24ba00> while a presentation or dismiss is in progress!

来自 IBAction 的片段

  self.rootRef.child("users/").child(NSUserDefaults.standardUserDefaults().stringForKey("id")!).observeEventType(.Value, withBlock: { snapshot in
        if (snapshot.value!.objectForKey("otherUserID") as? String)! != "" {

            print("This is being executed in the observer")

            self.dismissViewControllerAnimated(false, completion: nil)

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewControllerWithIdentifier("active")
            self.presentViewController(controller, animated: true, completion: nil)
        }
    })


override func viewDidDisappear(animated: Bool) {
    self.rootRef.child(NSUserDefaults.standardUserDefaults().stringForKey("id")!).removeAllObservers()
}

最佳答案

您可以使用observeSingleEventOfType(.Value, withBlock:,因此您不必删除观察者

此外,正如@Shubhank 指出的那样,您必须在您返回的 View Controller 中显示 VC。 self 无效,因为您已将其解雇,这就是警告的原因

关于ios - 观察者使用 removeAllObervers 多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38574694/

相关文章:

ios8 照片编辑扩展问题

ios - 相机预览层不显示 Swift 4

json - Swift JSONSerialization 将 double 值隐式转换为字符串

ios - Swift Firebase 在 child 身上获得值(value)

validation - 如何在 Firebase 中运行服务器端代码?

Android Studio ML 套件无法加载 OCR 模块

ios - 在 xcode 中,尝试向我的 ipad 添加代码时,说应用程序应用程序失败,与设备通信时出错。我如何解决它

iphone - 什么会导致 iphone 应用程序未报告的崩溃?

sql - 附加两个 SQLite-DB 文件以在 Swift 中查询这两个文件

ios - API 调用的本地通知