我有一个 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/