封装RAC的网络请求方式
+ (RACSignal *)rac_request:(NSString *)apiName parameters:(NSDictionary *)parameters showError:(BOOL)showError {
NSAssert(apiName != nil, @"identifier 不能为空");
Class requestClass = NSClassFromString(apiName);
NSAssert(requestClass != nil, @"未找到 %@ 类", apiName);
RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
YTKRequestWithAuth *api = [[requestClass alloc] init];
NSAssert([api isKindOfClass:[YTKRequestWithAuth class]], @"class %@ must be a subclass of YTKRequestWithAuth", apiName);
[api setRequestArgumentWithAuth:parameters];
[api setSuccessCompletionBlock:^(__kindof YTKRequestWithAuth *request){
if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
[subscriber sendNext:request.responseString];
} else {
[subscriber sendNext:request.responseJSONObject];
}
[subscriber sendCompleted];
}];
[api setFailureCompletionBlock:^(__kindof YTKRequestWithAuth *request) {
if (request.responseStatusCode == 200) {
if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
[subscriber sendNext:request.responseString];
} else {
[subscriber sendNext:request.responseJSONObject];
}
[subscriber sendCompleted];
} else if (request.authStrategy == AuthStrategyTokenWithSign && request.responseStatusCode == 403) {
NSLog(@"==== 重新申请token ========");
// 如果请求失败是由于认证未通过则获取新的token后retry请求
[AuthTokenManager refreshAuthTokenWithCompletion:^(NSString *token, NSError *tokenError) {
if (!tokenError) {
YTKRequestWithAuth *requestTwice = [[requestClass alloc] init];
requestTwice.requestArgumentWithAuth = request.requestArgumentWithAuth;
[requestTwice startWithCompletionBlockWithSuccess:request.successCompletionBlock failure:^(__kindof YTKRequestWithAuth *request2) {
if (request.responseStatusCode == 200) {
if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
[subscriber sendNext:request.responseString];
} else {
[subscriber sendNext:request.responseJSONObject];
}
[subscriber sendCompleted];
} else {
NSError *error = [self proccessResponseForRequest:request];
[subscriber sendError:error];
}
}];
} else {
// [subscriber sendError:tokenError];
}
}];
} else {
NSError *error = [self proccessResponseForRequest:request];
[subscriber sendError:error];
}
}];
[api startWithAgent:nil];
return [RACDisposable disposableWithBlock:^{
[api stop];
}];
}] replayLazily];
if (showError) {
[signal subscribeError:^(NSError *error) {
}];
}
return signal;
}
- (void)refreshData{
[[FYAPIClient rac_request:@"WebGetDialogDetailInfo" parameters:@{@"dialog_tid":self.model.TID} showError:YES]subscribeNext:^(id x) {
} completed:^{
[self refreshDataSource];
} ] ;
}
- (void)refreshDataSource{
NSDictionary *params;
if (pageIndex>0&&self.dataSource.count>0) {
LastTestMsgModel *model = self.dataSource.firstObject;
params = @{@"pagesize":@PAGESIZE,@"dialog_tid":self.model.TID,@"tid":model.TID};
}else{
params = @{@"pagesize":@PAGESIZE,@"dialog_tid":self.model.TID};
}
[[FYAPIClient rac_request:@"WebGetChatList" parameters:params showError:YES] subscribeNext:^(id x) {
}];
}
RAC网络封装以及多个网络请求嵌套
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.MVVM 代替 MVC 原因 无论MVC还是MVVM,包括其他设计模式,核心目的是为了提高代码的简洁性,降低耦...