To facilitate the maintenance of code after the MVVM refactoring, the following specifications have been developed:
First, Responsibility:
The responsibilities of VC (i.e. Viewcontroller):
1) Process the page layout and the life cycle of the page (viewdidload,viewwillappear, etc.)
2) respond to all interactions on the page, including button clicks, gestures, etc.
3) Listen to changes in the data in the VM, subscribe to the signal, according to the contents of the signal to make corresponding changes.
4) manage the creation and destruction of page-related VMS
Responsibilities for VMS (i.e., ViewModel):
1) Handle network requests, read and write local caches, and the corresponding view logic for these operations (including cue boxes, page jumps, etc.) signal input
2) Save the view to show the required data, including the data required for VC initialization
3) Handle other logic that does not depend on the page (such as saving a logged-in user name)
Ii. some rules for creating VMS:
1) One type of business creates only one VM
2) one page facing multiple businesses, multiple VMS can be created
3) The granularity of the division should be as thin as possible, so that similar business logic can use the same VM. Can be divided according to the network interface. (e.g. QMLOGINVM)
Third, VC and VM Communication mode:
Hook-up via REACTIVECOCOA signal
1) Network request unified with racsignal processing, in the signal block to execute the request, received a response to send next signal, pass status to the VC. Status is not saved in the VM.
2) VC subscription signal, according to the status value of next signal, to do the corresponding operation.
3) The display or close of the prompt box, the Enable state of the button, etc. can be processed by using the racsubject signal alone, the input signal from the VM, the VC subscription.
4) for TableView required data, unified in the VM to save data, VC through Racobserve or KVO to listen.
Iv. Block and delegate
The network framework supports both block and delegate two ways, the development of the module to differentiate, if the previous code is used delegate, continue to use delegate, and vice versa; if it is a newly developed module, select it according to your preference.
MVVM Code Refactoring specification