MVVM
is on MVC
the basis of the separation of business processing logic to the viewModel
layer.
M: The model layer is the raw data requested by the API, serving as a DTO (data transfer object), of course, with a dictionary is also possible, programming, to be flexible. The model layer is a relatively thin layer.
V: View layer, display, viewController
control, his task is to get the data from the ViewModel layer, and then display.
VMS: The ViewModel layer, which is responsible for business processing and data transformation, is the binder for the view and model layers, which is an excellent place for placing user input validation logic, view display logic, initiating network requests, and other kinds of code. To put it bluntly, the original Viewcontroller layer of business logic and page logic and so stripped out to the ViewModel layer.
Simply put, the API requests the data, parse model
it, then convert it into viewModel
data that can be used directly by the view layer and deliver it to the front end.
Model Layer
Let's start with the model layer, where we use Jsonmodel to parse, like the model length of a product list:
这是我们的数据原型,API返回的数据通过JSONModel解析完成后的原始数据存在这里。
#import <JSONModel/JSONModel.h>@protocol lvmproductlistmodel <NSObject>@end // productlist@interface** * * refprice ; @end
ViewModel Layer
The ViewModel layer is the core layer of our business logic, where we need to initiate a network request (if the network request is more, you can extract it, only call it in ViewModel), parse the data, and transform the data to the front end.
#pragmaMark-public methods-(void) Lvm_startloadproductlistwithpage: (Nsinteger) page {__weaktypeof(self) weakself =Self ; [NetWorkManager GET:self.lvm_baseURL parameters:parameters success:^ (Nsurlsessiondatatask *task,IDresponseobject) {__strongtypeof(weakself) strongself =weakself; ... Nsdictionary*resultdic = responseobject[@"Rp_result"]; Nserror*error =Nil; Lvmproductlistmodel*model = [[Lvmproductlistmodel alloc] Initwithdictionary:resultdic error:&ERROR]; if(Error) {...} [Strongself _lvm_calproductlists:model.productlist]; if(strangles.Delegate ...) { ... } } Failure:^ (Nsurlsessiondatatask *task, Nserror *error) { ... }];}- (void) _lvm_calproductlists: (Nsarray *) productlists{ for(Nsinteger i =0; i < Productlists.count; ++i) {Lvmproductlistmodel*model =Productlists[i]; Lvmproductlistitem*item =[[Lvmproductlistitem alloc] init]; Item.lvm_productid=Model.productid; Item.lvm_productname=Model.productname; Item.lvm_productprice= [NSString stringWithFormat:@"¥%@", Model.refprice]; Item.lvm_productimgurl= [Utils convertToRealUrl:model.imgUrl ofsize: -]; [Self.lvm_productlists Addobject:item]; }}
The viewModel
data returned by the API is parsed and model
model
converted into a view
layer that can be used directly by the tier and item
item
delivered to the front end.
viewModel
after the transformed data is item
viewModel
saved, data-related processing is viewModel
processed. viewModel
view
The interface returned to the layer looks like this:
@interface Lvmproductlistviewmodel (Collectionviewdatasource)- (Nsinteger) Lvm_numberofitemsinsection: ( Nsinteger) Section; -(Lvmproductlistitem *) Lvm_itemforindexpath: (Nsindexpath *) Indexpath; @end
View Layer
view
The layer is viewController
controlled by. view
layer only to do display, do not do business processing. view
layer is provided by the data viewModel
. view
the layer looks like this:
@implementationLvmproductlistviewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; [Self _lvm_initial]; [Self _lvm_setupviewmodel]; [Self _lvm_setupsubviews]; [Self.lvm_viewmodel lvm_startloadproductlistwithpage:_lvm_currentpage];}- (void) _lvm_initial {... self.lvm_currentpage=1;}- (void) _lvm_setupviewmodel {Self.lvm_viewmodel=[[Lvmproductlistviewmodel alloc] init]; _lvm_viewmodel.lvm_delegate=Self ;}#pragmaMark-subviews--(void) _lvm_setupsubviews {... [Self _lvm_setupcollectionview]; ...}- (void) _lvm_setupcollectionview {...}#pragmaMark-uicollectionview Delegate & datosource-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) Section {return[Self.lvm_viewmodel lvm_numberofitemsinsection:section];}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {Lvmproductlistitem*item =[Self.lvm_viewmodel Lvm_itemforindexpath:indexpath]; Lvmproductlistcollectionviewcell*cell = (Lvmproductlistcollectionviewcell *) [CollectionView dequeuereusablecellwithreuseidentifier:klvmproductlistcollectionviewcellid ForIndexPath: Indexpath]; [Cell Lvm_setupwithitem:item]; returncell;}
On the architecture design of MVVM in iOS