|
1 | 1 | # STNetTaskQueue |
2 | 2 | Queue for managing network request |
| 3 | + |
| 4 | +If you don't want to put all the network reqeust logics in a "Manager" class, **STNetTaskQueue** may be your choice. You can now handle each network reqeust with separated **STNetTask** instead. |
| 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. |
| 7 | + |
| 8 | +## Get Started |
| 9 | +#### Step 1: Setup STNetTaskQueue after your app launch |
| 10 | +```objective-c |
| 11 | +NSURL *baseUrl = [NSURL URLWithString:@"http://api.openweathermap.org"]; |
| 12 | +STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithQueue:[STNetTaskQueue sharedQueue] baseURL:baseUrl]; |
| 13 | +[STNetTaskQueue sharedQueue].handler = httpHandler; |
| 14 | +``` |
| 15 | +
|
| 16 | +#### Step 2: Write your net task for each reqeust |
| 17 | +```objective-c |
| 18 | +@interface STOpenWeatherNetTask : STHTTPNetTask |
| 19 | +
|
| 20 | +@property (nonatomic, strong) NSString *latitude; |
| 21 | +@property (nonatomic, strong) NSString *longitude; |
| 22 | +@property (nonatomic, strong, readonly) NSString *place; |
| 23 | +@property (nonatomic, assign, readonly) float temperature; |
| 24 | +
|
| 25 | +@end |
| 26 | +``` |
| 27 | + |
| 28 | +```objective-c |
| 29 | +@implementation STOpenWeatherNetTask |
| 30 | + |
| 31 | +- (STHTTPNetTaskMethod)method |
| 32 | +{ |
| 33 | + return STHTTPNetTaskGet; |
| 34 | +} |
| 35 | + |
| 36 | +- (NSString *)uri |
| 37 | +{ |
| 38 | + return @"data/2.5/weather"; |
| 39 | +} |
| 40 | + |
| 41 | +- (NSDictionary *)parameters |
| 42 | +{ |
| 43 | + return @{ @"lat": self.latitude, |
| 44 | + @"lon": self.longitude }; |
| 45 | +} |
| 46 | + |
| 47 | +- (void)didResponseJSON:(NSDictionary *)response |
| 48 | +{ |
| 49 | + _place = response[@"name"]; |
| 50 | + _temperature = [response[@"main"][@"temp"] floatValue] / 10; |
| 51 | +} |
| 52 | + |
| 53 | +@end |
| 54 | +``` |
| 55 | +
|
| 56 | +#### Step 3: Go and get your response |
| 57 | +```objective-c |
| 58 | +if (_openWeatherTask.pending) { |
| 59 | + return; |
| 60 | +} |
| 61 | +_openWeatherTask = [STOpenWeatherNetTask new]; |
| 62 | +_openWeatherTask.latitude = @"1.306038"; |
| 63 | +_openWeatherTask.longitude = @"103.772962"; |
| 64 | +// Task delegate will be a weak reference, so no need to remove it manually. |
| 65 | +// Duplicated task delegates will be ignored by STNetTaskQueue, so it's fine to invoke addTaskDelegate here. |
| 66 | +[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri]; |
| 67 | +[[STNetTaskQueue sharedQueue] addTask:_openWeatherTask]; |
| 68 | +``` |
| 69 | + |
| 70 | +```objective-c |
| 71 | +- (void)netTaskDidEnd:(STNetTask *)task |
| 72 | +{ |
| 73 | + // It's necessary to detect if _openWeatherTask != task and return, |
| 74 | + // if you have mutiple instance/viewController deleagating the same uri. |
| 75 | + if (_openWeatherTask != task) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (task.error) { // Would be network issue |
| 80 | + _resultLabel.text = @"Network Unavailable"; |
| 81 | + _goBtn.hidden = YES; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + _resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature]; |
| 86 | + _goBtn.hidden = YES; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +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. |
| 91 | + |