Skip to content

Commit 293afb1

Browse files
committed
Remove example project since it's outdated.
Use unit test as example in README.
1 parent 5f6c12a commit 293afb1

File tree

17 files changed

+80
-877
lines changed

17 files changed

+80
-877
lines changed

README.md

Lines changed: 37 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@ STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and
33

44
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.
55

6-
## Glance
7-
### Tired of this?
8-
```objc
9-
[network GET:@"data/2.5/weather" parameters:@{ @"lat": location.lat,
10-
@"lon": location.lon,
11-
@"user_info": location.userInfo,
12-
@"other_parameter": @"value" }];
13-
```
14-
### What about this?
15-
```objc
16-
STOpenWeatherNetTask *openWeatherTask = [STOpenWeatherNetTask new];
17-
openWeatherTask.location = location;
18-
[[STNetTaskQueue sharedQueue] addTask:openWeatherTask];
19-
```
20-
STNetTaskQueue will get all non-readonly properties from "location" and pack them to parameters for you. See [Get Started](https://github.com/kevin0571/STNetTaskQueue#get-started) for more details.
21-
226
## Features
237
- Auto packing parameters for HTTP net task.
248
- Max concurrent tasks count in each STNetTaskQueue.
@@ -60,34 +44,34 @@ github "kevin0571/STNetTaskQueue"
6044
### Use STNetTaskQueue in your project
6145
#### Step 1: Setup STNetTaskQueue after your app launch
6246
```objc
63-
NSURL *baseUrl = [NSURL URLWithString:@"http://api.openweathermap.org"];
47+
NSURL *baseUrl = [NSURL URLWithString:@"http://jsonplaceholder.typicode.com"];
6448
STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];
6549
[STNetTaskQueue sharedQueue].handler = httpHandler;
6650
```
6751
6852
#### Step 2: Create your net task
6953
```objc
70-
@interface STOpenWeatherNetTask : STHTTPNetTask
54+
@interface STTestPostNetTask : STHTTPNetTask
7155
72-
@property (nonatomic, strong) STLocation *location;
73-
@property (nonatomic, strong) NSString *userInfo;
74-
@property (nonatomic, strong, readonly) NSString *place;
75-
@property (nonatomic, assign, readonly) float temperature;
56+
@property (nonatomic, strong) NSString *title;
57+
@property (nonatomic, strong) NSString *body;
58+
@property (nonatomic, assign) int userId;
59+
@property (nonatomic, strong, readonly) NSDictionary *post;
7660
7761
@end
7862
```
7963

8064
```objc
81-
@implementation STOpenWeatherNetTask
65+
@implementation STTestPostNetTask
8266

8367
- (STHTTPNetTaskMethod)method
8468
{
85-
return STHTTPNetTaskGet;
69+
return STHTTPNetTaskPost;
8670
}
8771

8872
- (NSString *)uri
8973
{
90-
return @"data/2.5/weather";
74+
return @"posts";
9175
}
9276

9377
// Optional. Retry 3 times after error occurs.
@@ -120,86 +104,42 @@ STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] init
120104
return @{ @"other_parameter": @"value" };
121105
}
122106

123-
- (void)didResponseDictionary:(NSDictionary *)dictionary
107+
// Optional. Transform value to a format you want.
108+
- (id)transformValue:(id)value
124109
{
125-
_place = dictionary[@"name"];
126-
_temperature = [dictionary[@"main"][@"temp"] floatValue] / 10;
110+
if ([value isKindOfClass:[NSDate class]]) {
111+
return @([value timeIntervalSince1970]);
112+
}
113+
return value;
127114
}
128115

129-
@end
130-
```
131-
132-
#### Step 3: Send net task and delegate for the result
133-
```objc
134-
- (void)sendOpenWeatherTask
116+
- (void)didResponseDictionary:(NSDictionary *)dictionary
135117
{
136-
if (_openWeatherTask.pending) {
137-
return;
138-
}
139-
140-
STLocation *location = [STLocation new];
141-
location.lat = @"1.306038";
142-
location.lon = @"103.772962";
143-
location.ignoredValue = 1;
144-
145-
_openWeatherTask = [STOpenWeatherNetTask new];
146-
_openWeatherTask.location = location;
147-
_openWeatherTask.userInfo = @"user info";
148-
// STHTTPNetTask will pack non-readonly properties which is number, BOOL, NSString, NSDictionary, NSArray or object conforms to STHTTPNetTaskRequestObject, also parameters returned by overwritten method "parameters". Which means the final packed parameters would be:
149-
// @{ @"lat": @"1.306038",
150-
// @"lon": @"103.772962",
151-
// @"user_info": @"user info",
152-
// @"other_parameter": @"value" }
153-
154-
// Task delegate will be a weak reference, so there is no need to remove it manually.
155-
// It's appropriate to add task delegate here because duplicated task delegates will be ignored by STNetTaskQueue.
156-
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri];
157-
[[STNetTaskQueue sharedQueue] addTask:_openWeatherTask];
118+
_post = dictionary;
158119
}
159-
```
160-
161-
#### Create a request object
162-
Conform STHTTPNetTaskRequestObject protocol
163-
```objc
164-
@interface STLocation : NSObject<STHTTPNetTaskRequestObject>
165-
166-
@property (nonatomic, strong) NSString *lat;
167-
@property (nonatomic, strong) NSString *lon;
168-
@property (nonatomic, assign) int ignoredValue;
169-
@property (nonatomic, assign, readonly) BOOL readOnlyProperty; // Read only property will not be packed into parameters
170120

171121
@end
172122
```
173123
124+
#### Step 3: Send net task and delegate for the result
174125
```objc
175-
@implementation STLocation
176-
177-
// If you want to ignore some properties when packing the request object, return an array with property names.
178-
- (NSArray *)ignoredProperties
179-
{
180-
return @[ @"ignoredValue" ];
181-
}
182-
183-
// This is optional, if this is not implemented, underscore "_" will be used as separator when packing parameters. Which means if you use CamelCase naming for your property, it will be converted to lower cases string separated by "_", e.g. "userInfo" will be packed as "user_info" in parameters.
184-
- (NSString *)parameterNameSeparator
185-
{
186-
return @"_";
187-
}
188-
189-
@end
126+
STTestPostNetTask *testPostTask = [STTestPostNetTask new];
127+
testPostTask.title = @"Test Post Net Task Title";
128+
testPostTask.body = @"Test Post Net Task Body";
129+
testPostTask.userId = 1;
130+
testPostTask.date = [NSDate new];
131+
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];
132+
[[STNetTaskQueue sharedQueue] addTask:testPostTask];
190133
```
191134

192135
#### Use subscription block
193136
```objc
194-
[_openWeatherTask subscribeState:STNetTaskStateFinished usingBlock:^{
195-
if (task.error) { // Would be network issue
196-
_resultLabel.text = @"Network Unavailable";
197-
_goBtn.hidden = YES;
137+
[testPostTask subscribeState:STNetTaskStateFinished usingBlock:^{
138+
if (testPostTask.error) {
139+
// Handle error cases
198140
return;
199141
}
200-
201-
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature];
202-
_goBtn.hidden = YES;
142+
// Access result from net task
203143
}];
204144
```
205145
@@ -208,38 +148,27 @@ Conform STHTTPNetTaskRequestObject protocol
208148
```objc
209149
- (void)netTaskDidEnd:(STNetTask *)task
210150
{
211-
// It's necessary to detect if _openWeatherTask != task,
212-
// if you have mutiple viewControllers deleagating the same uri.
213-
if (_openWeatherTask != task) {
151+
if (task.error) {
152+
// Handle error cases
214153
return;
215154
}
216-
217-
if (task.error) { // Would be network issue
218-
_resultLabel.text = @"Network Unavailable";
219-
_goBtn.hidden = YES;
220-
return;
221-
}
222-
223-
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature];
224-
_goBtn.hidden = YES;
155+
// Access result from net task
225156
}
226157
```
227158

228159
#### Work with ReactiveCocoa for getting net task result
229160

230161
```objc
231-
[STNetTaskObserve(_openWeatherTask) subscribeCompleted:^(
232-
if (_openWeatherTask.error) { // Would be network issue
233-
_resultLabel.text = @"Network Unavailable";
234-
_goBtn.hidden = YES;
162+
[STNetTaskObserve(testPostTask) subscribeCompleted:^(
163+
if (testPostTask.error) {
164+
// Handle error cases
235165
return;
236166
}
237-
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature];
238-
_goBtn.hidden = YES;
167+
// Access result from net task
239168
}];
240169
```
241170
242-
For more details, download the example project or check out unit tests for usage references.
171+
For more details, check out unit tests.
243172
244173
### Set max concurrent tasks count of STNetTaskQueue
245174
Sometimes we need to set the concurrent image download tasks to avoid too much data coming at the same time.

0 commit comments

Comments
 (0)