STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols.
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.
- Max concurrent tasks count in each STNetTaskQueue.
- Max retry count for each STNetTask.
- Net task is cancelable after added to STNetTaskQueue.
- Multiple delegates for same net task.
- Works with ReactiveCocoa, subscribeNext for net task result.
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.
STNetTask is abstract, it provides basic properties and callbacks for subclassing.
STNetTaskDelegate is the delegate protocol for observing result of STNetTask, mostly it is used in view controller.
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.
platform :ios, '7.0'
pod 'STNetTaskQueue'NSURL *baseUrl = [NSURL URLWithString:@"http://api.openweathermap.org"];
STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];
[STNetTaskQueue sharedQueue].handler = httpHandler;@interface STOpenWeatherNetTask : STHTTPNetTask
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *longitude;
@property (nonatomic, strong, readonly) NSString *place;
@property (nonatomic, assign, readonly) float temperature;
@end@implementation STOpenWeatherNetTask
- (STHTTPNetTaskMethod)method
{
return STHTTPNetTaskGet;
}
- (NSString *)uri
{
return @"data/2.5/weather";
}
- (NSUInteger)maxRetryCount
{
return 3; // Retry after error occurs
}
- (BOOL)shouldRetryForError:(NSError *)error
{
return YES; // Retry for all kinds of errors
}
- (NSTimeInterval)retryInterval
{
return 5; // Retry after 5 seconds
}
- (NSDictionary *)headers
{
return @{ @"custom_header": @"value" };
}
- (NSDictionary *)parameters
{
return @{ @"lat": self.latitude, @"lon": self.longitude };
}
- (void)didResponseDictionary:(NSDictionary *)dictionary
{
_place = dictionary[@"name"];
_temperature = [dictionary[@"main"][@"temp"] floatValue] / 10;
}
@end- (void)sendOpenWeatherTask
{
if (_openWeatherTask.pending) {
return;
}
_openWeatherTask = [STOpenWeatherNetTask new];
_openWeatherTask.latitude = @"1.306038";
_openWeatherTask.longitude = @"103.772962";
// Task delegate will be a weak reference, so there is no need to remove it manually.
// It's appropriate to add task delegate here because duplicated task delegates will be ignored by STNetTaskQueue.
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri];
[[STNetTaskQueue sharedQueue] addTask:_openWeatherTask];
}[STNetTaskObserve(_openWeatherTask) subscribeNext:^(STOpenWeatherNetTask *task) {
if (task.error) { // Would be network issue
_resultLabel.text = @"Network Unavailable";
_goBtn.hidden = YES;
return;
}
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", task.place, task.temperature];
_goBtn.hidden = YES;
}];- (void)netTaskDidEnd:(STNetTask *)task
{
// It's necessary to detect if _openWeatherTask != task,
// if you have mutiple viewControllers deleagating the same uri.
if (_openWeatherTask != task) {
return;
}
if (task.error) { // Would be network issue
_resultLabel.text = @"Network Unavailable";
_goBtn.hidden = YES;
return;
}
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature];
_goBtn.hidden = YES;
}For more details, download the example project or check out unit tests for usage references.
Sometimes we need to set the concurrent image download tasks to avoid too much data coming at the same time.
STNetTaskQueue *downloadQueue = [STNetTaskQueue new];
downloadQueue.handler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com"]];
downloadQueue.maxConcurrentTasksCount = 2;
/*
[downloadQueue addTask:task1];
[downloadQueue addTask:task2];
[downloadQueue addTask:task3]; // task3 will be sent after task1 or task2 is finished.
*/- More unit tests for STHTTPNetTaskQueueHandler.
- Detailed documentation for STNetTaskQueue, STNetTask, STNetTaskChain.