0

I want to receive the same callback in the ViewController that is opened at in the time that server response in my Swift Application.

I have two ViewControllers. The first ViewController registers a callBack from a class "NetworkService".

The second ViewController is Opened from the first ViewController and the second receives the "NetworkService" from the firstViewController initialized in a variable, and then registers the same callBack.

When I try to receive the callback from the server, if the first ViewController is opened I get the response. If I open the second ViewController and I resend the response I get this correctly in the second ViewController.

BUT if I return to the first ViewController and I get the response, its' only received on the Second ViewController all times.

class NetworkService {

    var onFunction: ((_ result: String)->())?

    func doCall() {
        self.onFunction?("result")
    }

}


class MyViewController: UIViewController {

    let networkService = NetworkService()

    override func viewDidLoad() {
        super.viewDidLoad()

        networkService.onFunction = { result in
            print("I got \(result) from the server!")
        }

    }
}

I open the secondViewController like:

let vc = self.storyboard!.instantiateViewController(withIdentifier: "second") as! SecondViewController
vc. networkService = networkService
        self.navigationController?.pushViewController(vc, animated: true)

And the Second ViewController:

class SecondViewController: UIViewController {

    var networkService: NetworkService?

    override func viewDidLoad() {
        super.viewDidLoad()

        networkService!.onFunction = { result in
            print("I got \(result) from the server!")
        }

    }
}

How would it be possible to receive the response in the first ViewController again, then return to first ViewController from the second calling the popViewController?

self.navigationController?.popViewController(animated: false)  
Levi Yoder
  • 177
  • 9
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 1
    Why aren't you trying `NSNotification` instead of a callback. That way you are going receive the callback to all observers of that notification irrespective of where you are. – Ayan Sengupta Apr 23 '19 at 21:58

2 Answers2

0

How about calling the function within viewDidAppear on both ViewControllers so that you get your response every time you switch between the two views? You wouldn't need to pass networkService between the ViewControllers.

override func viewDidAppear(_ animated: Bool) {

  networkService!.onFunction = { result in
            print("I got \(result) from the server!")
        }

}
Levi Yoder
  • 177
  • 9
0

You can use notification but you will have to register and deregister VC as you switch between views. Other option is to use delegate, you will need to share NetworkService instance. Quick example of how this could work with protocol.

protocol NetworkServiceProtocol {
    var service: NetworkService? { get }
    func onFunction(_ result: String)
}

class NetworkService {

    var delegate: NetworkServiceProtocol?

    func doCall() {
        self.delegate?.onFunction("results")
    }

    func update(delegate: NetworkServiceProtocol) {
        self.delegate = delegate
    }
}

class VC1: UIViewController, NetworkServiceProtocol {
    var service: NetworkService?

    init(service: NetworkService? = nil) {
        self.service = service
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.service?.update(delegate: self)
    }

    func onFunction(_ result: String) {
        print("On Function")
    }
}
k-thorat
  • 4,873
  • 1
  • 27
  • 36