2

I have an app that use http calls to stream video from external storage. When the user's device isn't connected to a network service, I need the app to go back to the previous controller.

the flow is the following: the user access a list of elements (table view cell), select one, then the app goes to the player controller. On this controller, the call is made to stream the file.

I use an api call handler in a class outside of the controller and I don't know how to proceed to make it go back to the previous controller from here (the element list).

Connectivity issues errors are all catched within the api class. I don't show any code as Im not sure it would be relevant. If you need to see anything, let me know and I will update the question. What should be the way to do that? (of course I use a navigation controller)

Thanks

SKYnine
  • 2,708
  • 7
  • 31
  • 41

2 Answers2

2

If you want go back to the previous view controller you should use:

navigationController?.popViewControllerAnimated(true)

If you need to use this function not in the view-controller but in another class you can use NSNotificationCenter for notify the view-controller when it's needed to show the previous controller, just like this:

YourViewController

override func viewDidLoad() 
{
    ...

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "goBack:",
        name: "goBackNotification",
        object: nil)

    ...
}

func goBack(notification: NSNotification)
{
    navigationController?.popViewControllerAnimated(true)
}

AnotherClass

NSNotificationCenter.defaultCenter().postNotificationName("goBackNotification", object: nil)

Don't forget to remove the observer in your YourViewController:

deinit 
{
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

EDIT 1: you can use obviously a delegate instead of a NSNotification method. If you don't know the differences between NSNotification and delegate I recommend to you this answer.

Community
  • 1
  • 1
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
0

A common approach besides NSNotificationCenter is to utilize closures or delegates to inform your ViewController that the streaming attempt failed. Using closures, the API of the class responsible for the streaming could be extended to take a completion closure as parameter, and call it with an NSError, if one occurred, or nil if it didn't.

func streamFile(completion: NSError? -> Void) {
    // Try to start the streaming and call the closure with an error or nil
    completion(errorOrNil)
}

When the call to the API in the ViewController is made you can then pass a closure to the method and check for errors. In case something went wrong an error should be present and the ViewController should be dismissed

func startStream() {
    StreamingAPIClient.streamFile(completion: { [weak self] error in
        if error != nil {
            // Handle error
            self.dismissViewControllerAnimated(true, completion: nil)
        } else {
            // Proceed with the streaming
        }
    })
}
Daehn
  • 547
  • 6
  • 12