AlexanderHart
AlexanderHart

Reputation: 77

Observer Fires Multiple Times with removeAllObervers

I have an IBAction that when pressed, it creates an observer listener and gets snapshot data.

When a certain child node changes value, I want to dismiss the current ViewController and present a different ViewController.

When the ViewController gets dismissed, I am attempting to remove all observer handles.

But when I build and run, judging from my console output, it seems that my Firebase Observer is being called multiple times for unknown.

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!

Snippet from 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()
}

Upvotes: 0

Views: 198

Answers (1)

Dmytro Rostopira
Dmytro Rostopira

Reputation: 11165

You can use observeSingleEventOfType(.Value, withBlock:, so you don't have to remove observer

Also, as @Shubhank pointed, you have to present the VC in the viewcontroller you dismissed back to. the self isn't valid to present since you have dismissed it and that's why the warning

Upvotes: 4

Related Questions