This structure is suitable for scenarios: version iteration is frequent, function is complex, developers are not fixed, and it is difficult to test and maintain. In test maintenance, the error code can be located more quickly for the error part. For those who take over the project halfway, the composition of this module can be better understood.
XcodeThe VIP development template of Scene provides the templates of ViewController, Interactor, Presenter, Worker, Router and Model, in which Scene can create a complete VIP structure.
Responsible for presentation of views
It acts asPresenterOfoutput，fromPresenterAcquired
ViewModelTo assign data to views,ViewControllerIt does not process data in any way, but only from
ViewModelIt shows whatever data it gives.
At the same time, it also acts as aInteractorOfinput，When the user interacts with the interface, it takes this event and the parameters needed to handle it as one
Requestprovide forInteractor，Give WayInteractorTo handle this interactive event.
Handling events that occur when users interact with the interface
It acts asViewControllerOfoutput，WhenViewControllerInteractive events are triggered with the user.InteractorWill come fromViewControllerProvided
RequestIn fact, it is a manager of event processing. There are many workers working for it, such as the workers who request the negative network, the workers who query the database. These workers are the real handlers.The basic unit of the document (more detailed pictures will be introduced later).
At the same time, it also acts as aPresenterOfinput，When the worker gets the data, the result is encapsulated into one
ResponseIt is then provided toPresenterTo deal with.
Data processing into the content of the view to be displayed
It acts asInteractorOfoutput，takeInteractorProvidedResponseProcessing, sometimes we just need toResponseSome of the data, or some sort of data processing, are done here, and the data is encapsulated after processing.ViewModel。
At the same time, it also acts as aViewControllerOfinput，byViewControllerThe view is presented with the necessary and processed data.
The above is introduced.InteractorAs mentioned, they are in theInteractorThe basic unit in which events are really handled, eachWorkerThey have a single responsibility. For tasks requiring network requests, invoke those responsible for network requestsWorker，If you need to update or retrieve the database, you can call the one responsible for the database processing.Worker，We can define many different types of events by event type.Worker。WorkerAfter processing, the processing is called back toInteractor。
When inViewControllerWhen interacting with users, fromViewControllerreachInteractorUntil thenPresentrtFinally backViewControllerThere will be a series of
ViewModel。For each particular event, there is a specific
ViewModel，So inModelsIn Swift, we will use Swift’s unique namespace to define a namespace for each event, which contains
- Request：Some parameters needed for network requests or database queries.
- Response：Some data returned by network requests or database queries.
- ViewModel：Processing Response into data that the view needs to display.
Routers, as the name implies, are used for page hopping. stayRouterInside, we need to pass it according to the situation.
segueJump, or pass
XIBTo create, or directly through code creation, and initialize the new controller assignment, and finally jump. For the initialization assignment of the new controller, we passRouterInside
DataStoreAttributes are acquired, and as suchRouterOf
DataStoreNamelyInteractor，Because page Jump also belongs to user interaction, it is a jump event. Before jumping, it needs to be handled before jumping, after processing.RouterIn fromInteractorGet data to initialize the controller.
Refer to Link: