|
1 | 1 | # STNetTaskQueue |
2 | | -Queue for managing network requests |
| 2 | +STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols. |
3 | 3 |
|
4 | | -STNetTaskQueue may be your choice if you want to handle each network request stuff in separated STNetTask instead of having all the network requests logics in a "Manager" class. |
| 4 | +STNetTaskQueue avoid you from directly dealing with "url", "request packing" and "response parsing". All networking tasks are described and processed by subclassing STNetTask, which provides you a clean code style in UI layer when handling networking. |
5 | 5 |
|
6 | | -**STHTTPNetTaskQueueHandler** is included, which is for HTTP based network reqeust. If you are looking for a socket or other protocol based handler, currently you should write your own net task queue handler and conform to **STNetTaskQueueHandler** protocol. **STHTTPNetTaskQeueuHandler** depends on [AFNetworking](https://github.com/AFNetworking/AFNetworking), which is included in example project. |
| 6 | +## STHTTPNetTaskQueueHandler |
7 | 7 |
|
8 | | -## Features |
9 | | -- Retry net task with specified max retry count. |
10 | | -- Delegate for net task result according to "uri" of net task. |
11 | | -- More features are coming as STNetTaskQueue is extendible. |
| 8 | +STHTTPNetTaskQueueHandler is a HTTP based implementation of STNetTaskQueueHandler. It provides different ways to pack request and parse response, e.g. STHTTPNetTaskRequestJSON is for JSON format request body, STHTTPNetTaskResponseJSON is for JSON format response data and STHTTPNetTaskRequestFormData is for form data format request body which is mostly used for uploading file. |
12 | 9 |
|
13 | | -## Sequence Chart |
14 | | - |
| 10 | +## STNetTask |
15 | 11 |
|
16 | | -## Get Started |
| 12 | +STNetTask is abstract, it provides basic properties and callbacks for subclassing. |
| 13 | + |
| 14 | +## STNetTaskDelegate |
| 15 | + |
| 16 | +STNetTaskDelegate is the delegate protocol for observing result of STNetTask, mostly it is used in view controller. |
17 | 17 |
|
18 | | -### Installation with CocoaPods |
| 18 | +## STNetTaskChain |
19 | 19 |
|
20 | | -[CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like AFNetworking in your projects. See the ["Getting Started" guide for more information](https://github.com/AFNetworking/AFNetworking/wiki/Getting-Started-with-AFNetworking). |
| 20 | +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. |
| 21 | + |
| 22 | +## Get Started |
21 | 23 |
|
22 | | -#### Podfile |
| 24 | +### Podfile |
23 | 25 |
|
24 | 26 | ```ruby |
25 | 27 | platform :ios, '7.0' |
26 | | -pod 'STNetTaskQueue', '~> 0.0.1' |
| 28 | +pod 'STNetTaskQueue', '~> 0.0.2' |
27 | 29 | ``` |
28 | 30 |
|
29 | | - |
| 31 | +### Use STNetTaskQueue in your project |
30 | 32 | #### Step 1: Setup STNetTaskQueue after your app launch |
31 | 33 | ```objc |
32 | 34 | NSURL *baseUrl = [NSURL URLWithString:@"http://api.openweathermap.org"]; |
33 | 35 | STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl]; |
34 | 36 | [STNetTaskQueue sharedQueue].handler = httpHandler; |
35 | 37 | ``` |
36 | 38 |
|
37 | | -#### Step 2: Write your net task for each reqeust |
| 39 | +#### Step 2: Create your net task |
38 | 40 | ```objc |
39 | 41 | @interface STOpenWeatherNetTask : STHTTPNetTask |
40 | 42 |
|
@@ -61,7 +63,7 @@ STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] init |
61 | 63 |
|
62 | 64 | - (NSUInteger)maxRetryCount |
63 | 65 | { |
64 | | - return 3; |
| 66 | + return 3; // Retry after error occurs |
65 | 67 | } |
66 | 68 |
|
67 | 69 | - (NSDictionary *)parameters |
@@ -79,25 +81,28 @@ STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] init |
79 | 81 | @end |
80 | 82 | ``` |
81 | 83 |
|
82 | | -#### Step 3: Go and get your response |
| 84 | +#### Step 3: Send net task and delegate for the result |
83 | 85 | ```objc |
84 | | -if (_openWeatherTask.pending) { |
85 | | - return; |
| 86 | +- (void)sendOpenWeatherTask |
| 87 | +{ |
| 88 | + if (_openWeatherTask.pending) { |
| 89 | + return; |
| 90 | + } |
| 91 | + _openWeatherTask = [STOpenWeatherNetTask new]; |
| 92 | + _openWeatherTask.latitude = @"1.306038"; |
| 93 | + _openWeatherTask.longitude = @"103.772962"; |
| 94 | + // Task delegate will be a weak reference, so there is no need to remove it manually. |
| 95 | + // It's appropriate to add task delegate here because duplicated task delegates will be ignored by STNetTaskQueue. |
| 96 | + [[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri]; |
| 97 | + [[STNetTaskQueue sharedQueue] addTask:_openWeatherTask]; |
86 | 98 | } |
87 | | -_openWeatherTask = [STOpenWeatherNetTask new]; |
88 | | -_openWeatherTask.latitude = @"1.306038"; |
89 | | -_openWeatherTask.longitude = @"103.772962"; |
90 | | -// Task delegate will be a weak reference, so no need to remove it manually. |
91 | | -// Duplicated task delegates will be ignored by STNetTaskQueue, so it's fine to invoke addTaskDelegate here. |
92 | | -[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri]; |
93 | | -[[STNetTaskQueue sharedQueue] addTask:_openWeatherTask]; |
94 | 99 | ``` |
95 | 100 |
|
96 | 101 | ```objc |
97 | 102 | - (void)netTaskDidEnd:(STNetTask *)task |
98 | 103 | { |
99 | | - // It's necessary to detect if _openWeatherTask != task and return, |
100 | | - // if you have mutiple instance/viewController deleagating the same uri. |
| 104 | + // It's necessary to detect if _openWeatherTask != task, |
| 105 | + // if you have mutiple viewControllers deleagating the same uri. |
101 | 106 | if (_openWeatherTask != task) { |
102 | 107 | return; |
103 | 108 | } |
@@ -112,5 +117,10 @@ _openWeatherTask.longitude = @"103.772962"; |
112 | 117 | _goBtn.hidden = YES; |
113 | 118 | } |
114 | 119 | ``` |
| 120 | +For more details, download the example project or check out unit tests for usage references. |
| 121 | + |
| 122 | +## What's Next |
115 | 123 |
|
116 | | -You can see more details in example project. The example is tested with iOS SDK 8.1, XCode 6.1.1 and iPhone 6 simulator. |
| 124 | +- More unit tests for STHTTPNetTaskQueueHandler. |
| 125 | +- Detailed documentation for STNetTaskQueue, STNetTask, STNetTaskChain. |
| 126 | +- Support other protocol based STNetTaskQueueHandler, e.g. STNetTaskQueueHandler for ProtocolBuffers. |