-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHYBatchContinueRequest.m
More file actions
162 lines (135 loc) · 3.62 KB
/
HYBatchContinueRequest.m
File metadata and controls
162 lines (135 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// HYBatchContinueRequest.m
// YTKNetworkDemo
//
// Created by zheng on 2017/8/17.
// Copyright © 2017年 yuantiku.com. All rights reserved.
//
#import "HYBatchContinueRequest.h"
#import "HYNetworkPrivate.h"
#import "HYBatchContinueRequestAgent.h"
#import "HYRequest.h"
@interface HYBatchContinueRequest() <HYRequestDelegate>
@property (nonatomic) NSInteger finishedCount;
@property (nonatomic) NSInteger failedCount;
@property (nonatomic, readwrite, strong) NSMutableArray *failedRequestArray;
@end
@implementation HYBatchContinueRequest
- (instancetype)initWithRequestArray:(NSArray<HYRequest *> *)requestArray
{
self = [super init];
if (self)
{
_requestArray = [requestArray copy];
_failedRequestArray = [NSMutableArray array];
_finishedCount = 0;
_failedCount = 0;
for (HYRequest * req in _requestArray)
{
if (![req isKindOfClass:[HYRequest class]])
{
NSLog(@"Error, request item must be YTKRequest instance.");
return nil;
}
}
}
return self;
}
- (void)start
{
if (_finishedCount > 0 || _failedCount > 0)
{
NSLog(@"Error! Batch request has already started.");
return;
}
[_failedRequestArray removeAllObjects];
// [[HYBatchRequestAgent sharedAgent] addBatchRequest:self];
for (HYRequest * req in _requestArray)
{
req.delegate = self;
//单个request 的回调统一为BatchCallback,不需要自己的回调
[req clearCompletionBlock];
[req start];
}
}
- (void)stop
{
_delegate = nil;
[self clearRequest];
// [[HYBatchRequestAgent sharedAgent] removeBatchRequest:self];
}
- (void)clearRequest
{
for (HYRequest * req in _requestArray)
{
[req stop];
}
[_failedRequestArray removeAllObjects];
[self clearCompletionBlock];
}
- (void)startWithCompletionBlockWithCompletion:(nullable void (^)(HYBatchContinueRequest *batchRequest))completion
{
[self setCompletionBlockWithCompletion:completion];
[self start];
}
- (void)setCompletionBlockWithCompletion:(nullable void (^)(HYBatchContinueRequest *batchRequest))completion
{
self.completionBlock = completion;
}
- (void)clearCompletionBlock
{
self.completionBlock = nil;
}
- (BOOL)isDataFromCache
{
BOOL result = YES;
for (HYRequest *request in _requestArray)
{
if (!request.isDataFromCache)
{
result = NO;
}
}
return result;
}
- (void)dealloc
{
[self clearRequest];
}
#pragma mark - HYRequestDelegate
- (void)requestFinished:(HYRequest *)request
{
_finishedCount++;
[self handleRequestDelegateCallBack];
}
- (void)requestFailed:(HYRequest *)request
{
_failedCount ++;
if ([_delegate respondsToSelector:@selector(batchContinueRequestFailed:failedRequest:)])
{
[_delegate batchContinueRequestFailed:self failedRequest:request];
}
[_failedRequestArray addObject:request];
[self handleRequestDelegateCallBack];
}
- (void)handleRequestDelegateCallBack
{
if (_finishedCount + _failedCount == _requestArray.count)
{
if ([_delegate respondsToSelector:@selector(batchContinueRequestFinished:failedRequest:)])
{
[_delegate batchContinueRequestFinished:self failedRequest:_failedRequestArray];
}
if (_completionBlock)
{
_completionBlock(self);
}
[self clearCompletionBlock];
[[HYBatchContinueRequestAgent sharedAgent] removeBatchRequest:self];
}
}
- (NSArray *)failedRequestArray
{
return [_failedRequestArray copy];
}
@end