diff --git a/LCNetwork.podspec b/LCNetwork.podspec index e752a18..fcab904 100644 --- a/LCNetwork.podspec +++ b/LCNetwork.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "LCNetwork" - s.version = "1.1.7" + s.version = "1.1.8" s.summary = "基于AFNetworking的网络库封装" s.homepage = "https://github.com/bawn/LCNetwork" s.license = 'MIT' diff --git a/LCNetwork/LCBaseRequest.h b/LCNetwork/LCBaseRequest.h index 5119c64..ef43500 100644 --- a/LCNetwork/LCBaseRequest.h +++ b/LCNetwork/LCBaseRequest.h @@ -194,9 +194,13 @@ typedef NS_ENUM(NSInteger , LCRequestSerializerType) { */ @property (nonatomic, strong) id responseJSONObject; /** - * 原始数据 + * 接口返回的原始数据 */ -@property (nonatomic, strong) id rawJSONObject; +@property (nonatomic, strong, readonly) id rawJSONObject; +/** + * 是否不执行插件,默认是 NO, 也就是说当添加了插件默认是执行,比如有时候需要隐藏HUD + */ +@property (nonatomic, assign) BOOL invalidAccessory; @property (nonatomic, strong, readonly) id cacheJson; @property (nonatomic, strong, readonly) NSString *urlString; @property (nonatomic, strong, readonly) NSMutableArray *requestAccessories; diff --git a/LCNetwork/LCBaseRequest.m b/LCNetwork/LCBaseRequest.m index 721ee26..99995fb 100644 --- a/LCNetwork/LCBaseRequest.m +++ b/LCNetwork/LCBaseRequest.m @@ -189,25 +189,31 @@ - (void)addAccessory:(id)accessory { @implementation LCBaseRequest (RequestAccessory) - (void)toggleAccessoriesWillStartCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStart:)]) { - [accessory requestWillStart:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStart:)]) { + [accessory requestWillStart:self]; + } } } } - (void)toggleAccessoriesWillStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStop:)]) { - [accessory requestWillStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStop:)]) { + [accessory requestWillStop:self]; + } } } } - (void)toggleAccessoriesDidStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestDidStop:)]) { - [accessory requestDidStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestDidStop:)]) { + [accessory requestDidStop:self]; + } } } } diff --git a/LCNetwork/LCBatchRequest.h b/LCNetwork/LCBatchRequest.h index 259689d..51d7d0e 100644 --- a/LCNetwork/LCBatchRequest.h +++ b/LCNetwork/LCBatchRequest.h @@ -39,6 +39,10 @@ @interface LCBatchRequest : NSObject +/** + * 是否不执行插件,默认是 NO, 也就是说当添加了插件默认是执行,比如有时候需要隐藏HUD + */ +@property (nonatomic, assign) BOOL invalidAccessory; @property (nonatomic, strong, readonly) NSArray *requestArray; @property (nonatomic, copy) void (^successCompletionBlock)(LCBatchRequest *); @property (nonatomic, copy) void (^failureCompletionBlock)(LCBatchRequest *); diff --git a/LCNetwork/LCBatchRequest.m b/LCNetwork/LCBatchRequest.m index 2f735b0..2ca4e9a 100644 --- a/LCNetwork/LCBatchRequest.m +++ b/LCNetwork/LCBatchRequest.m @@ -30,6 +30,7 @@ @interface LCBatchRequest () @property (nonatomic) NSInteger finishedCount; +@property (nonatomic, strong) NSArray *requestArray; @end @@ -75,6 +76,15 @@ - (void)stop{ [[LCBatchRequestAgent sharedInstance] removeBatchRequest:self]; } +- (void)requestDidStop{ + [self clearCompletionBlock]; + [self toggleAccessoriesDidStopCallBack]; + self.finishedCount = 0; + self.requestArray = nil; + [[LCBatchRequestAgent sharedInstance] removeBatchRequest:self]; +} + + - (void)startWithCompletionBlockWithSuccess:(void (^)(LCBatchRequest *request))success failure:(void (^)(LCBatchRequest *request))failure { [self setCompletionBlockWithSuccess:success failure:failure]; @@ -94,7 +104,6 @@ - (void)setCompletionBlockWithSuccess:(void (^)(LCBatchRequest *batchRequest))su } - (void)clearCompletionBlock { - // nil out to break the retain cycle. self.successCompletionBlock = nil; self.failureCompletionBlock = nil; } @@ -116,30 +125,22 @@ - (void)requestFinished:(LCBaseRequest *)request { if (_successCompletionBlock) { _successCompletionBlock(self); } - [self clearCompletionBlock]; - [self toggleAccessoriesDidStopCallBack]; - [[LCBatchRequestAgent sharedInstance] removeBatchRequest:self]; + [self requestDidStop]; } } - (void)requestFailed:(LCBaseRequest *)request { [self toggleAccessoriesWillStopCallBack]; - // Stop for (LCBaseRequest *req in _requestArray) { [req stop]; } - // Callback if ([_delegate respondsToSelector:@selector(batchRequestFailed:)]) { [_delegate batchRequestFailed:self]; } if (_failureCompletionBlock) { _failureCompletionBlock(self); } - // Clear - [self clearCompletionBlock]; - - [self toggleAccessoriesDidStopCallBack]; - [[LCBatchRequestAgent sharedInstance] removeBatchRequest:self]; + [self requestDidStop]; } - (void)clearRequest { @@ -164,25 +165,31 @@ - (void)addAccessory:(id)accessory { @implementation LCBatchRequest (RequestAccessory) - (void)toggleAccessoriesWillStartCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStart:)]) { - [accessory requestWillStart:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStart:)]) { + [accessory requestWillStart:self]; + } } } } - (void)toggleAccessoriesWillStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStop:)]) { - [accessory requestWillStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStop:)]) { + [accessory requestWillStop:self]; + } } } } - (void)toggleAccessoriesDidStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestDidStop:)]) { - [accessory requestDidStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestDidStop:)]) { + [accessory requestDidStop:self]; + } } } } diff --git a/LCNetwork/LCChainRequest.h b/LCNetwork/LCChainRequest.h index 4fca401..e4f9e66 100644 --- a/LCNetwork/LCChainRequest.h +++ b/LCNetwork/LCChainRequest.h @@ -43,6 +43,10 @@ typedef void (^LCChainCallback)(LCChainRequest *chainRequest, __kindof LCBaseReq @interface LCChainRequest : NSObject +/** + * 是否不执行插件,默认是 NO, 也就是说当添加了插件默认是执行,比如有时候需要隐藏HUD + */ +@property (nonatomic, assign) BOOL invalidAccessory; @property (nonatomic, weak) id delegate; @property (nonatomic, strong) NSMutableArray *requestAccessories; diff --git a/LCNetwork/LCChainRequest.m b/LCNetwork/LCChainRequest.m index 2c7b2a5..df20e16 100644 --- a/LCNetwork/LCChainRequest.m +++ b/LCNetwork/LCChainRequest.m @@ -65,6 +65,9 @@ - (void)start { } } +- (NSArray *)requestArray{ + return [self.requestArray copy]; +} - (void)stop { [self toggleAccessoriesWillStopCallBack]; @@ -73,6 +76,15 @@ - (void)stop { [self toggleAccessoriesDidStopCallBack]; } +- (void)requestDidStop{ + [self toggleAccessoriesDidStopCallBack]; + self.nextRequestIndex = 0; + [self.requestCallbackArray removeAllObjects]; + [_requestArray removeAllObjects]; + [[LCChainRequestAgent sharedInstance] removeChainRequest:self]; +} + + - (void)addRequest:(LCBaseRequest *)request callback:(LCChainCallback)callback { [_requestArray addObject:request]; if (callback != nil) { @@ -82,9 +94,6 @@ - (void)addRequest:(LCBaseRequest *)request callback:(LCChainCallback)callback { } } -- (NSArray *)requestArray { - return _requestArray; -} - (BOOL)startNextRequest { if (_nextRequestIndex < [_requestArray count]) { @@ -93,7 +102,8 @@ - (BOOL)startNextRequest { request.delegate = self; [request start]; return YES; - } else { + } + else { return NO; } } @@ -108,13 +118,12 @@ - (void)requestFinished:(LCBaseRequest *)request { NSUInteger currentRequestIndex = _nextRequestIndex - 1; LCChainCallback callback = _requestCallbackArray[currentRequestIndex]; callback(self, request); - if (![self startNextRequest]) { + if ([self startNextRequest] == NO) { [self toggleAccessoriesWillStopCallBack]; if ([_delegate respondsToSelector:@selector(chainRequestFinished:)]) { [_delegate chainRequestFinished:self]; } - [self toggleAccessoriesDidStopCallBack]; - [[LCChainRequestAgent sharedInstance] removeChainRequest:self]; + [self requestDidStop]; } } @@ -122,9 +131,8 @@ - (void)requestFailed:(LCBaseRequest *)request { [self toggleAccessoriesWillStopCallBack]; if ([_delegate respondsToSelector:@selector(chainRequestFailed:failedBaseRequest:)]) { [_delegate chainRequestFailed:self failedBaseRequest:request]; - [[LCChainRequestAgent sharedInstance] removeChainRequest:self]; } - [self toggleAccessoriesDidStopCallBack]; + [self requestDidStop]; } - (void)clearRequest { @@ -152,25 +160,31 @@ - (void)addAccessory:(id)accessory { @implementation LCChainRequest (RequestAccessory) - (void)toggleAccessoriesWillStartCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStart:)]) { - [accessory requestWillStart:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStart:)]) { + [accessory requestWillStart:self]; + } } } } - (void)toggleAccessoriesWillStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestWillStop:)]) { - [accessory requestWillStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestWillStop:)]) { + [accessory requestWillStop:self]; + } } } } - (void)toggleAccessoriesDidStopCallBack { - for (id accessory in self.requestAccessories) { - if ([accessory respondsToSelector:@selector(requestDidStop:)]) { - [accessory requestDidStop:self]; + if (self.invalidAccessory == NO) { + for (id accessory in self.requestAccessories) { + if ([accessory respondsToSelector:@selector(requestDidStop:)]) { + [accessory requestDidStop:self]; + } } } } diff --git a/LCNetwork/LCNetworkAgent.m b/LCNetwork/LCNetworkAgent.m index e5182c0..b64e692 100644 --- a/LCNetwork/LCNetworkAgent.m +++ b/LCNetwork/LCNetworkAgent.m @@ -96,7 +96,6 @@ - (void)addRequest:(LCBaseRequest *)request { self.manager.requestSerializer.timeoutInterval = [request.child requestTimeoutInterval]; } - // 是否使用自定义超时时间 if ([request.child respondsToSelector:@selector(requestTimeoutInterval)]) { self.manager.requestSerializer.timeoutInterval = [request.child requestTimeoutInterval]; @@ -113,9 +112,7 @@ - (void)addRequest:(LCBaseRequest *)request { }]; } else if ([request.child requestMethod] == LCRequestMethodPost){ - // multipart POST request if ([request.child respondsToSelector:@selector(constructingBodyBlock)] && [request.child constructingBodyBlock]) { - request.sessionDataTask = [self.manager POST:url parameters:argument constructingBodyWithBlock:[request.child constructingBodyBlock] progress:^(NSProgress * _Nonnull uploadProgress) { [self handleRequestProgress:uploadProgress request:request]; } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -220,7 +217,7 @@ - (void)handleRequestFailure:(NSURLSessionDataTask *)sessionDataTask{ if (request) { [request toggleAccessoriesWillStopCallBack]; if (request.delegate != nil) { - [request.delegate requestFinished:request]; + [request.delegate requestFailed:request]; } if (request.failureCompletionBlock) { request.failureCompletionBlock(request); diff --git a/LCNetworkDemo/LCNetworkDemo/LCRequestAccessory.m b/LCNetworkDemo/LCNetworkDemo/LCRequestAccessory.m index f93dd89..b6d616a 100644 --- a/LCNetworkDemo/LCNetworkDemo/LCRequestAccessory.m +++ b/LCNetworkDemo/LCNetworkDemo/LCRequestAccessory.m @@ -36,11 +36,11 @@ - (void)requestWillStart:(id)request { } - (void)requestWillStop:(id)request { - + [self.hud hide:NO]; } - (void)requestDidStop:(id)request{ - [self.hud hide:NO]; +// [self.hud hide:NO]; } @end