So I've had an issue recently that I haven't quite figured out how to solve. My app is setup in the following way:
- Two Views, A and B (they are not parent / child)
- One Engine Class (that does some data manipulation)
- Network Layer Class
- DB
The view creates an engine and sets itself as the delegate. The engine creates the request and passes it to the network class which then reports back to the engine class through a delegate. The engine does whatever it needs to do, stores it in the DB then, through a delegate, informs the view.
This is all fine and dandy and has no need for singletons and everything is great. However, the problem is that the network call to View B takes some time. And I'd like to call it while the user is still on View A. Since each view keeps it's own instance of the Engine, I can call the network from View A, but the instance of the engine on View B will not be aware of it.
I was thinking in this instance that the engine class could send an NSNotification once the network call is processed. This way even though there are two different instances of the engine the View would get notified. I'd LOVE to use a singleton but our architect hates them with a passion.
I do not know if I understand, but it looks like you can use NotificationCenter to make this communication, like you said
so, in your view class, you can add an notification observer, that will call some method of your view, for exemple update the UI: (in swift 4)
In your engine class, when you receive the response of the request, you can post the notification with the same name that you put on view class, you pass any data on userInfo dictionary if necessary:
like you said, any instance of the view class will call the “updateViewMethod" method, if you want to send separate notifications, you have to specify the name of the notification when instantiating the view and engine for A and for B, for example, in your view and engine class you could create an variable named “notificationName" and put different values for A and B instance, so you view class notification should be like:
and the engine:
hope have helped in some way.