preface
This paper mainly explains the framework currently used in our project, the framework used is MVCS, and the network request is YTKNetwork. Welcome to leave a message below, because the project may need to be reconstructed now, and I am just a little white and have no specific optimization plan.
The principle of
MVC is not enough to explain, we just added a Service layer on this basis, to handle the network request, and callback processing.
www.jianshu.com/p/8b0d06bd5… Blog.csdn.net/wangyanchan…
code
I wrote a demo, the basic with the similar project framework, the problem is the controller inside a lot of callback methods as well as the initialization method, coupling between modules is too high, which is just like going to the login page, and then returning from the login page you need to determine when a lot of conditions, conditions of each execution of the callback methods may not be consistent, Not easy to maintain. The demo effect is as follows:
M layer
@interface HomeModel : ResponseModel
@property (nonatomic, copy) NSString *iconImg;
@property (nonatomic, copy) NSString *iconName;
@property (nonatomic, copy) NSString *iconDesc;
@property (nonatomic, assign) int type;
@end
Copy the code
ResponseModel is inherited from JSONModel
The Service layer
It is mainly used to process requests, because the core idea of YTKNetwork is to encapsulate each request into a class, and then call it through the Service or Controller layer
Protocol The method used to initialize data, written as a Protocol because multiple pages may need it
@protocol HomeDataProtocol <NSObject>
- (void)initWithData:(id)data;
- (void)initWithData:(id)data selectIndex:(NSIndexPath *)indexPath;
@end
Copy the code
This is the proxy method for clicking the addition and subtraction corresponding to the cell
@protocol HomeTableViewCellDelegate <NSObject>
- (void)didClickPlusView:(UIButton *)plusView index:(NSInteger)index;
- (void)didClickMinusView:(UIButton *)minusView index:(NSInteger)index;
@end
Copy the code
RequestService, which calls the Request method, may contain methods used in the home page module
@implementation HomeRequestService + (void)requestWithUserId:(NSString *)userId complete:(void(^)(id result,NSError *error))complete{ HomeRequest *request = [[HomeRequest alloc] init]; request.userId = userId; [request requestComplete:^(BaseRequest * _Nonnull request) {// Because parsing job can completely unified processing [request handleRequestWithClass: NSArray. Class withComplete: ^ (id _Nonnull model, NSError * _Nonnull error) {if(complete) { model = [HomeModel arrayOfModelsFromDictionaries:model error:&error]; complete(model,error); }}]; }]; } @endCopy the code
Request This inherited Request from YTKNetwork. The.m file needs to implement the secondary path of the Request, whether to add cache, Request parameters, etc
@implementation HomeRequest
- (BOOL)enableMockData{/// YES means to use the local cache. It will find the HomeRequest. Json file from the local cache for parsing, in order to test the data source modificationreturnYES; } - (NSString *)requestUrl{/// This is the secondary path of the request. The primary path needs to be configured at application startupreturn @"XXXXX/XXXX/AA.do"; } - (YTKRequestMethod)requestMethod{/// this is the requestMethodreturnYTKRequestMethodPOST; } - (id)requestArgument{/// this is the requestArgumentreturn@ {@"userId":_userId};
}
@end
Copy the code
The Controller layer
Mainly responsible for the display of page UI, as well as request data, including the implementation of proxy method, due to the amount of code, only part of the code is listed
@interface HomeViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
HomeTableViewCellDelegate
>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
- (void)requestData{
__weak typeof(self)weakSelf = self;
[HomeRequestService requestWithUserId:@"test"
complete:^(id _Nonnull result, NSError * _Nonnull error) {
if(! Error) {/// initialize data [weakself. dataArray removeAllObjects]; [weakSelf.dataArray addObjectsFromArray:result]; [weakSelf.tableView reloadData]; }else{/// handle pop-ups or something}}]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.delegate = self; [cell initWithData:self.dataArray[indexPath.row] selectIndex:indexPath];returncell; } didClickPlusView:(UIButton *)plusView index:(NSInteger)index{HomeModel *model = self.dataArray[index]; model.type++; [self.tableView reloadRow:indexinSection:0
withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableView *)tableView{
if(! _tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.backgroundColor = [UIColor redColor]; [_tableView registerClass:[HomeTableViewCell class]forCellReuseIdentifier:identifier];
}
return _tableView;
}
Copy the code
The View layer is just a statement, the creation of the UITableViewCell
The base class
@interface BaseRequest: YTKRequesttrueRepresents using - (BOOL)enableMockData; // @param complete returns the result - (void)requestComplete:(void(^)(BaseRequest *request))complete; // @param clsName is the class that needs to be processed. // @param complete returns the result (void)handleRequestWithClass:(id)clsName withComplete:(void(^)(id model,NSError *error))complete; @endCopy the code
As for the implementation, the method calling YTKNetwork and a parsing method calling jsonModel will not be described too much
conclusion
advantages
1. The project structure is relatively clear and easy to understand and use.
2. Reduced a lot of judgment, such as the judgment of request parameters, which is handed over to YTKNetwork, the judgment of return data parsing, which is handed over to JSONModel, and can also be added for a specific request cache.
3, you can separately configure a request to use local JSON, in the interface is not implemented when you can extract development, as long as there is a structure.
disadvantages
1. If the page UI is complex and there are many event interactions, the amount of code in the Controller layer will increase dramatically. Most of them are delegate methods and getter methods.
2. There will be more project classes, because each request is a separate class. At present, there are about 100 interfaces in the project, and the class of light request is 100+, not counting the Service layer.
3. The coupling degree between modules is still very high, and the current architecture is not suitable for module decoupling.