美文网首页
MVVM与网络请求

MVVM与网络请求

作者: 凌云01 | 来源:发表于2020-10-21 19:22 被阅读0次

MVVM

MVVM.png
  • View:展示界面、用户交互、接收通过Pipeline 传递的用户数据,用于展示与更新
  • ViewController:分发pipeline、跳转(通过路由 openURL)等
  • ViewModel:初始化Pipeline,业务逻辑处理(网络请求、数据处理与解析),监听pipeline 判断是否触发view进行数据更新或者其他操作
  • Pipeline:页面展示的数据

- (void)addObservers {
    @weakify(self);
    [IGCObserve(self.pipeline, ###) handle:^(id newValue) {
        @strongify(self);
        if ([newValue boolValue]) {
            self.pipeline.pageIndex = 1;
            [self fetchDataWithLoadMore:NO];
        }
    }];
}

addObservers

设置路由

#define COMPONENT_NAME @"igetcool://juvenile.dedao.live/chineseclass/liveclass/preview"

#pragma mark - IGCComponentRegisterProtocol
- (NSString *)componentName {
    return COMPONENT_NAME;
}

+ (NSDictionary<NSString *, IGCComponentConfig *> *)createComNameKeyAndComConfigObj {
    return @{COMPONENT_NAME: [IGCComponentConfig quickInitComponentWithViewModelClassName:NSStringFromClass([IGCPreviewViewModel class]) viewClassName:NSStringFromClass([IGCPreviewView class]) componentName:@"语文课学习资料预览列表页"]};
}

网络请求 IGCNetworking

[IGCNetworking sendRequest:^(XMRequest * _Nonnull request) {

}plugin:^(IGCNetworkingPlugin * _Nonnull plugin) {
    
} onSuccess:^(id  _Nullable responseObject, id  _Nullable model) {
   
 } onFailure:^(NSError * _Nullable error) {
    
} onFinished:^(id  _Nullable responseObject, NSError * _Nullable error) {
   
 }];

一、发起一个网络请求,采用默认插件

+ (NSString *)sendRequest:(XMRequestConfigBlock)configBlock
                onSuccess:(nullable IGCNetworkingSuccessBlock)successBlock
                onFailure:(nullable XMFailureBlock)failureBlock;

二、发起一个网络请求,采用默认插件

+ (NSString *)sendRequest:(XMRequestConfigBlock)configBlock
                onSuccess:(nullable IGCNetworkingSuccessBlock)successBlock
                onFailure:(nullable XMFailureBlock)failureBlock
               onFinished:(nullable XMFinishedBlock)finishedBlock;

三、发起一个网络请求,可定义插件

+ (NSString *)sendRequest:(XMRequestConfigBlock)configBlock
                   plugin:(nullable IGCNetworkingPluginBlock)pluginBlock
                onSuccess:(nullable IGCNetworkingSuccessBlock)successBlock
                onFailure:(nullable XMFailureBlock)failureBlock;

四、发起一个网络请求,可定义插件

+ (NSString *)sendRequest:(XMRequestConfigBlock)configBlock
                   plugin:(nullable IGCNetworkingPluginBlock)pluginBlock
                onSuccess:(nullable IGCNetworkingSuccessBlock)successBlock
                onFailure:(nullable XMFailureBlock)failureBlock
               onFinished:(nullable XMFinishedBlock)finishedBlock;

五、发起批量网络请求,可定义插件。

含有 成功,失败,完成的回调。
request:
完全由外部进行制定
plugin:
完全由外部制定

+ (nullable NSString *)sendBatchRequest:(XMBatchRequestConfigBlock)configBlock
                                plugins:(IGCNetworkingBatchPluginsBlock)pluginsBlock
                              onSuccess:(nullable IGCNetworkingBatchSuccessPluginsBlock)successBlock
                              onFailure:(nullable XMBCFailureBlock)failureBlock
                             onFinished:(nullable XMBCFinishedBlock)finishedBlock;

  • XMRequestConfigBlock: 请求配置 parameters、server、api、httpMethod、requestSerializerType

  • IGCNetworkingPluginBlock: 插件配置
    1、JSON 转 model (jsonToModelClassName
    2、缓存cacheKeycacheKey为nil,不缓存,不为nil时,则进行缓存处理,如果指定了jsonToModelClassName,按对应model进行缓存,否则按对应原始数据data缓存)
    3、hud:默认为No
    4、commonProcessForUserTokenError:通用 token 失效处理
    5、commonProcessForBusinessError:通用业务错误处理
    6、commonProcessForNetworkError:通用网络错误处理

  • IGCNetworkingSuccessBlock:响应成功block

  • XMFailureBlock:请求失败 error

  • XMFinishedBlock:请求已结束

相关文章

网友评论

      本文标题:MVVM与网络请求

      本文链接:https://www.haomeiwen.com/subject/dgvsmktx.html