Skip to content

Commit

Permalink
新增插件是否需要执行的属性
Browse files Browse the repository at this point in the history
  • Loading branch information
bawn committed Mar 3, 2016
1 parent c2015d3 commit ffe42b8
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 56 deletions.
2 changes: 1 addition & 1 deletion LCNetwork.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
8 changes: 6 additions & 2 deletions LCNetwork/LCBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 15 additions & 9 deletions LCNetwork/LCBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,31 @@ - (void)addAccessory:(id<LCRequestAccessory>)accessory {
@implementation LCBaseRequest (RequestAccessory)

- (void)toggleAccessoriesWillStartCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
}
}
}
}

- (void)toggleAccessoriesWillStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
}
}
}
}

- (void)toggleAccessoriesDidStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions LCNetwork/LCBatchRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
47 changes: 27 additions & 20 deletions LCNetwork/LCBatchRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@interface LCBatchRequest () <LCRequestDelegate>

@property (nonatomic) NSInteger finishedCount;
@property (nonatomic, strong) NSArray *requestArray;

@end

Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -164,25 +165,31 @@ - (void)addAccessory:(id<LCRequestAccessory>)accessory {
@implementation LCBatchRequest (RequestAccessory)

- (void)toggleAccessoriesWillStartCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
}
}
}
}

- (void)toggleAccessoriesWillStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
}
}
}
}

- (void)toggleAccessoriesDidStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions LCNetwork/LCChainRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<LCChainRequestDelegate> delegate;
@property (nonatomic, strong) NSMutableArray *requestAccessories;

Expand Down
50 changes: 32 additions & 18 deletions LCNetwork/LCChainRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ - (void)start {
}
}

- (NSArray *)requestArray{
return [self.requestArray copy];
}

- (void)stop {
[self toggleAccessoriesWillStopCallBack];
Expand All @@ -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) {
Expand All @@ -82,9 +94,6 @@ - (void)addRequest:(LCBaseRequest *)request callback:(LCChainCallback)callback {
}
}

- (NSArray *)requestArray {
return _requestArray;
}

- (BOOL)startNextRequest {
if (_nextRequestIndex < [_requestArray count]) {
Expand All @@ -93,7 +102,8 @@ - (BOOL)startNextRequest {
request.delegate = self;
[request start];
return YES;
} else {
}
else {
return NO;
}
}
Expand All @@ -108,23 +118,21 @@ - (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];
}
}

- (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 {
Expand Down Expand Up @@ -152,25 +160,31 @@ - (void)addAccessory:(id<LCRequestAccessory>)accessory {
@implementation LCChainRequest (RequestAccessory)

- (void)toggleAccessoriesWillStartCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStart:)]) {
[accessory requestWillStart:self];
}
}
}
}

- (void)toggleAccessoriesWillStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestWillStop:)]) {
[accessory requestWillStop:self];
}
}
}
}

- (void)toggleAccessoriesDidStopCallBack {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
if (self.invalidAccessory == NO) {
for (id<LCRequestAccessory> accessory in self.requestAccessories) {
if ([accessory respondsToSelector:@selector(requestDidStop:)]) {
[accessory requestDidStop:self];
}
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions LCNetwork/LCNetworkAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ - (void)addRequest:(LCBaseRequest <LCAPIRequest>*)request {
self.manager.requestSerializer.timeoutInterval = [request.child requestTimeoutInterval];
}


// 是否使用自定义超时时间
if ([request.child respondsToSelector:@selector(requestTimeoutInterval)]) {
self.manager.requestSerializer.timeoutInterval = [request.child requestTimeoutInterval];
Expand All @@ -113,9 +112,7 @@ - (void)addRequest:(LCBaseRequest <LCAPIRequest>*)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) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions LCNetworkDemo/LCNetworkDemo/LCRequestAccessory.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ffe42b8

Please sign in to comment.