简介
升级后基本没什么问题,现在把实现的主要流程上的改动贴上来(请结合我的上一篇文章查看)。
改动后网络层(包括文件上传的流程)并没有修改大的逻辑。
1.首先是一般网络POST请求的封装。
(1)首先是单例的实现基础设置:
+ (instancetype)sharedClient
{
static NetworkSessionManager *_sharedClient = nil;
static dispatch_once_t _onceTocken;
dispatch_once(&_onceTocken, ^{
if (appDelegate.apiBaseUrlStr) {
_sharedClient = [[NetworkSessionManager alloc] initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",appDelegate.apiBaseUrlStr]]];
} else {
_sharedClient = [[NetworkSessionManager alloc] init];
}
_sharedClient.llOperationQueue = [[NSOperationQueue alloc] init];
//最大并发数
_sharedClient.llOperationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
[_sharedClient.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[_sharedClient.requestSerializer setValue:ApiVersion forHTTPHeaderField:@"User-Agent"];
[_sharedClient.requestSerializer setTimeoutInterval:30];
_sharedClient.bstart = NO;
_sharedClient.apiCount = 0;
});
return _sharedClient;
}
(2)POST请求对外接口:
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nonnull, id _Nonnull))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{
if (![CommonClass checkNetworkRechable]) {
failure(nil,[NSError errorWithDomain:@"www.mHealth.com" code:-1009 userInfo:nil]);
return nil;
}
if((!appDelegate.apiBaseUrlStr || appDelegate.apiBaseUrlStr.length < 1) && !_bstart)
{
#ifdef DEBUG_MODE
NSLog(@"No baseurl, need network access");
#endif
[self.llOperationQueue setSuspended:true];
LandbalancerSessionOperation *llsessionOperation = [[LandbalancerSessionOperation alloc] initWithURLString:URLString parameters:parameters success:success failure:failure];
[self.llOperationQueue addOperation:llsessionOperation];
_bstart = true;
[self requestApi];
return nil;
}
else
{
return [super POST:URLString parameters:parameters progress:uploadProgress success:success failure:failure];
}
}
核心的改动就是这些了,当然了,因为我们的程序网络调用不规范,经过几代人的手,有很多地方都有了各自的调用,还有同一套代码有多个app在使用,所以在修改的时候也免不了许多麻烦。