Skip to content

Commit 7b3ed0e

Browse files
committed
Retain net task group to avoid being autoreleased.
Update README.md
1 parent 8d99bb2 commit 7b3ed0e

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ STNetTask is abstract, it provides basic properties and callbacks for subclassin
2323

2424
STNetTaskDelegate is the delegate protocol for observing result of STNetTask, mostly it is used in view controller.
2525

26-
## STNetTaskChain
26+
## ~~STNetTaskChain~~ (Deprecated. Use STNetTaskGroup instead)
2727

28-
STNetTaskChain is a chain which processes an array of STNetTask serially. A net task chain is considered as successful only if all net tasks in the chain are end without error.
28+
~~STNetTaskChain is a chain which processes an array of STNetTask serially. A net task chain is considered as successful only if all net tasks in the chain are end without error.~~
29+
30+
## STNetTaskGroup
31+
32+
A net task group for executing net tasks serially or concurrently.
2933

3034
## Get Started
3135

@@ -207,7 +211,41 @@ downloadQueue.maxConcurrentTasksCount = 2;
207211
*/
208212
```
209213

210-
## What's Next
214+
### Use STNetTaskGroup to execute multiple net tasks
215+
STNetTaskGroup supports two modes: STNetTaskGroupModeSerial and STNetTaskGroupConcurrent.
216+
STNetTaskGroupModeSerial will execute a net task after the previous net task is finished.
217+
STNetTaskGroupModeConcurrent will execute all net tasks concurrently.
218+
```objc
219+
STTestGetNetTask *task1 = [STTestGetNetTask new];
220+
task1.id = 1;
221+
222+
STTestGetNetTask *task2 = [STTestGetNetTask new];
223+
task2.id = 2;
224+
225+
STNetTaskGroup *group = [[STNetTaskGroup alloc] initWithTasks:@[ task1, task2 ] mode:STNetTaskGroupModeSerial];
226+
[group subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
227+
if (error) {
228+
// One of the net task is failed.
229+
return;
230+
}
231+
// All net tasks are finished without error.
232+
}];
233+
[group start];
234+
```
211235
212-
- More unit tests for STHTTPNetTaskQueueHandler.
213-
- Detailed documentation for STNetTaskQueue, STNetTask, STNetTaskChain.
236+
Or a handy way:
237+
```objc
238+
STTestGetNetTask *task1 = [STTestGetNetTask new];
239+
task1.id = 1;
240+
241+
STTestGetNetTask *task2 = [STTestGetNetTask new];
242+
task2.id = 2;
243+
244+
[[@[ task1, task2 ] subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
245+
if (error) {
246+
// One of the net task is failed.
247+
return;
248+
}
249+
// All net tasks are finished without error.
250+
}] start];
251+
```

STNetTaskQueue/STNetTaskGroup.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#import "STNetTaskGroup.h"
1010
#import "STNetTaskQueue.h"
1111

12+
static NSMutableSet<STNetTaskGroup *> *_retainedGroups; // Retain group to make sure it won't be autoreleased.
13+
1214
@interface STNetTaskGroup ()
1315

1416
@property (nonatomic, strong) STNetTask *executingTask;
@@ -62,21 +64,26 @@ - (STNetTaskGroup *)subscribeState:(STNetTaskGroupState)state usingBlock:(STNetT
6264
- (void)notifyState:(STNetTaskGroupState)state withError:(NSError *)error
6365
{
6466
NSMutableArray<STNetTaskGroupSubscriptionBlock> *blocks = self.stateToBlock[@(state)];
65-
for (STNetTaskSubscriptionBlock block in blocks) {
67+
for (STNetTaskGroupSubscriptionBlock block in blocks) {
6668
block(self, error);
6769
}
6870
self.stateToBlock = nil;
6971
self.taskSubscriptionBlock = nil;
72+
[_retainedGroups removeObject:self];
7073
}
7174

7275
- (void)start
7376
{
74-
NSAssert(!self.started, @"STNetTaskQueue can not be reused, please create a new instance.");
77+
NSAssert(!self.started, @"STNetTaskGroup can not be reused, please create a new instance.");
7578
if (self.pending) {
7679
return;
7780
}
7881
self.pending = YES;
7982
self.started = YES;
83+
if (!_retainedGroups) {
84+
_retainedGroups = [NSMutableSet new];
85+
}
86+
[_retainedGroups addObject:self];
8087

8188
switch (self.mode) {
8289
case STNetTaskGroupModeSerial: {

0 commit comments

Comments
 (0)