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.
- Auto packing parameters for HTTP net task.
- 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, subscribeCompleted 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.
A net task group for executing net tasks serially or concurrently.
platform :ios, '7.0'
pod 'STNetTaskQueue'
github "kevin0571/STNetTaskQueue"
NSURL *baseUrl = [NSURL URLWithString:@"http://jsonplaceholder.typicode.com"];
STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];
[STNetTaskQueue sharedQueue].handler = httpHandler;
@interface STTestPostNetTask : STHTTPNetTask
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *body;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, assign) int userId;
@property (nonatomic, strong) NSString<STIgnore> *ignored; // This property is ignored when packing the request.
@property (nonatomic, strong, readonly) NSDictionary *post;
@end
@implementation STTestPostNetTask
- (STHTTPNetTaskMethod)method
{
return STHTTPNetTaskPost;
}
- (NSString *)uri
{
return @"posts";
}
// Optional. Retry 3 times after error occurs.
- (NSUInteger)maxRetryCount
{
return 3;
}
// Optional. Retry for all types of errors
- (BOOL)shouldRetryForError:(NSError *)error
{
return YES;
}
// Optional. Retry after 5 seconds.
- (NSTimeInterval)retryInterval
{
return 5;
}
// Optional. Custom headers.
- (NSDictionary *)headers
{
return @{ @"custom_header": @"value" };
}
// Optional. Add parameters which are not inclued in requestObject and net task properties.
- (NSDictionary *)parameters
{
return @{ @"other_parameter": @"value" };
}
// Optional. Transform value to a format you want.
- (id)transformValue:(id)value
{
if ([value isKindOfClass:[NSDate class]]) {
return @([value timeIntervalSince1970]);
}
return value;
}
- (void)didResponseDictionary:(NSDictionary *)dictionary
{
_post = dictionary;
}
@end
STTestPostNetTask *testPostTask = [STTestPostNetTask new];
testPostTask.title = @"Test Post Net Task Title";
testPostTask.body = @"Test Post Net Task Body";
testPostTask.userId = 1;
testPostTask.date = [NSDate new];
testPostTask.ignored = @"test";
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];
[[STNetTaskQueue sharedQueue] addTask:testPostTask];
// The net task will be sent as described below.
/*
URI: posts
Method: POST
Request Type: Key-Value String
Response Type: JSON
Custom Headers:
{
"custom_header" = value;
}
Parameters:
{
body = "Test Post Net Task Body";
date = "1452239110.829915";
"other_parameter" = value;
title = "Test Post Net Task Title";
"user_id" = 1;
}
*/
[testPostTask subscribeState:STNetTaskStateFinished usingBlock:^{
if (testPostTask.error) {
// Handle error cases
return;
}
// Access result from net task
}];
- (void)netTaskDidEnd:(STNetTask *)task
{
if (task.error) {
// Handle error cases
return;
}
// Access result from net task
}
[STNetTaskObserve(testPostTask) subscribeCompleted:^(
if (testPostTask.error) {
// Handle error cases
return;
}
// Access result from net task
}];
For more details, check out unit tests.
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.
*/
STNetTaskGroup supports two modes: STNetTaskGroupModeSerial and STNetTaskGroupConcurrent. STNetTaskGroupModeSerial will execute a net task after the previous net task is finished. STNetTaskGroupModeConcurrent will execute all net tasks concurrently.
STTestGetNetTask *task1 = [STTestGetNetTask new];
task1.id = 1;
STTestGetNetTask *task2 = [STTestGetNetTask new];
task2.id = 2;
STNetTaskGroup *group = [[STNetTaskGroup alloc] initWithTasks:@[ task1, task2 ] mode:STNetTaskGroupModeSerial];
[group subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
if (error) {
// One of the net task is failed.
return;
}
// All net tasks are finished without error.
}];
[group start];
Or a handy way:
STTestGetNetTask *task1 = [STTestGetNetTask new];
task1.id = 1;
STTestGetNetTask *task2 = [STTestGetNetTask new];
task2.id = 2;
[[[@[ task1, task2 ] serialNetTaskGroup] subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
if (error) {
// One of the net task is failed.
return;
}
// All net tasks are finished without error.
}] start];