{"id":21302599,"url":"https://github.com/kevin-lyn/stnettaskqueue","last_synced_at":"2025-07-11T20:31:50.429Z","repository":{"id":27074148,"uuid":"30540611","full_name":"kevin-lyn/STNetTaskQueue","owner":"kevin-lyn","description":"STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols.","archived":false,"fork":false,"pushed_at":"2016-10-24T06:22:28.000Z","size":636,"stargazers_count":86,"open_issues_count":3,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-08T14:18:27.526Z","etag":null,"topics":["ios","macos","network","network-layer","networking"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kevin-lyn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-09T14:49:25.000Z","updated_at":"2023-11-14T08:12:15.000Z","dependencies_parsed_at":"2022-09-03T10:22:03.003Z","dependency_job_id":null,"html_url":"https://github.com/kevin-lyn/STNetTaskQueue","commit_stats":null,"previous_names":["kevin0571/stnettaskqueue"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/kevin-lyn/STNetTaskQueue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevin-lyn%2FSTNetTaskQueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevin-lyn%2FSTNetTaskQueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevin-lyn%2FSTNetTaskQueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevin-lyn%2FSTNetTaskQueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kevin-lyn","download_url":"https://codeload.github.com/kevin-lyn/STNetTaskQueue/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevin-lyn%2FSTNetTaskQueue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264892349,"owners_count":23679275,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","macos","network","network-layer","networking"],"created_at":"2024-11-21T15:56:56.172Z","updated_at":"2025-07-11T20:31:50.053Z","avatar_url":"https://github.com/kevin-lyn.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# STNetTaskQueue ![CI Status](https://img.shields.io/travis/kevin0571/STNetTaskQueue.svg?style=flat) ![Version](http://img.shields.io/cocoapods/v/STNetTaskQueue.svg?style=flag) ![License](https://img.shields.io/cocoapods/l/STNetTaskQueue.svg?style=flag)\nSTNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols.\n\nSTNetTaskQueue 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.\n\n## Features\n- Auto packing parameters for HTTP net task.\n- Max concurrent tasks count in each STNetTaskQueue.\n- Max retry count for each STNetTask.\n- Net task is cancelable after added to STNetTaskQueue.\n- Multiple delegates for same net task.\n- Works with ReactiveCocoa, subscribeCompleted for net task result.\n\n## STHTTPNetTaskQueueHandler\n\nSTHTTPNetTaskQueueHandler 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.\n\n## STNetTask\n\nSTNetTask is abstract, it provides basic properties and callbacks for subclassing.\n\n## STNetTaskDelegate\n\nSTNetTaskDelegate is the delegate protocol for observing result of STNetTask, mostly it is used in view controller. \n\n## ~~STNetTaskChain~~ (Deprecated. Use STNetTaskGroup instead)\n\n~~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.~~\n\n## STNetTaskGroup\n\nA net task group for executing net tasks serially or concurrently.\n\n## Get Started\n\n### Podfile\n\n```ruby\nplatform :ios, '7.0'\npod 'STNetTaskQueue'\n```\n\n### Carthage\n```ruby\ngithub \"kevin0571/STNetTaskQueue\"\n```\n\n### Use STNetTaskQueue in your project\n#### Step 1: Setup STNetTaskQueue after your app launch\n```objc\nNSURL *baseUrl = [NSURL URLWithString:@\"http://jsonplaceholder.typicode.com\"];\nSTHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];\n[STNetTaskQueue sharedQueue].handler = httpHandler;\n```\n\n#### Step 2: Create your net task\n```objc\n@interface STTestPostNetTask : STHTTPNetTask\n\n@property (nonatomic, strong) NSString *title;\n@property (nonatomic, strong) NSString *body;\n@property (nonatomic, strong) NSDate *date;\n@property (nonatomic, assign) int userId;\n@property (nonatomic, strong) NSString\u003cSTIgnore\u003e *ignored; // This property is ignored when packing the request.\n@property (nonatomic, strong, readonly) NSDictionary *post;\n\n@end\n```\n\n```objc\n@implementation STTestPostNetTask\n\n- (STHTTPNetTaskMethod)method\n{\n    return STHTTPNetTaskPost;\n}\n\n- (NSString *)uri\n{\n    return @\"posts\";\n}\n\n// Optional. Retry 3 times after error occurs.\n- (NSUInteger)maxRetryCount\n{\n    return 3;\n}\n\n// Optional. Retry for all types of errors\n- (BOOL)shouldRetryForError:(NSError *)error\n{\n    return YES;\n}\n\n// Optional. Retry after 5 seconds.\n- (NSTimeInterval)retryInterval\n{\n    return 5;\n}\n\n// Optional. Custom headers.\n- (NSDictionary *)headers\n{\n    return @{ @\"custom_header\": @\"value\" };\n}\n\n// Optional. Add parameters which are not inclued in requestObject and net task properties.\n- (NSDictionary *)parameters\n{\n    return @{ @\"other_parameter\": @\"value\" };\n}\n\n// Optional. Transform value to a format you want.\n- (id)transformValue:(id)value\n{\n    if ([value isKindOfClass:[NSDate class]]) {\n        return @([value timeIntervalSince1970]);\n    }\n    return value;\n}\n\n- (void)didResponseDictionary:(NSDictionary *)dictionary\n{\n    _post = dictionary;\n}\n\n@end\n```\n\n#### Step 3: Send net task and delegate for the result\n```objc\nSTTestPostNetTask *testPostTask = [STTestPostNetTask new];\ntestPostTask.title = @\"Test Post Net Task Title\";\ntestPostTask.body = @\"Test Post Net Task Body\";\ntestPostTask.userId = 1;\ntestPostTask.date = [NSDate new];\ntestPostTask.ignored = @\"test\";\n[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];\n[[STNetTaskQueue sharedQueue] addTask:testPostTask];\n\n// The net task will be sent as described below.\n/*\n    URI: posts\n    Method: POST\n    Request Type: Key-Value String\n    Response Type: JSON\n    Custom Headers:\n    {\n        \"custom_header\" = value;\n    }\n    Parameters:\n    {\n        body = \"Test Post Net Task Body\";\n        date = \"1452239110.829915\";\n        \"other_parameter\" = value;\n        title = \"Test Post Net Task Title\";\n        \"user_id\" = 1;\n    }\n */\n```\n\n#### Use subscription block\n```objc\n[testPostTask subscribeState:STNetTaskStateFinished usingBlock:^{\n    if (testPostTask.error) {\n        // Handle error cases\n        return;\n    }\n    // Access result from net task\n}];\n```\n\n#### Use STNetTaskDelegate\n\n```objc\n- (void)netTaskDidEnd:(STNetTask *)task\n{\n    if (task.error) {\n        // Handle error cases\n        return;\n    }\n    // Access result from net task\n}\n```\n\n#### Work with ReactiveCocoa for getting net task result\n\n```objc\n[STNetTaskObserve(testPostTask) subscribeCompleted:^(\n    if (testPostTask.error) {\n        // Handle error cases\n        return;\n    }\n    // Access result from net task\n}];\n```\n\nFor more details, check out unit tests.\n\n### Set max concurrent tasks count of STNetTaskQueue\nSometimes we need to set the concurrent image download tasks to avoid too much data coming at the same time.\n\n```objc\nSTNetTaskQueue *downloadQueue = [STNetTaskQueue new];\ndownloadQueue.handler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:[NSURL URLWithString:@\"http://example.com\"]];\ndownloadQueue.maxConcurrentTasksCount = 2;\n/*\n[downloadQueue addTask:task1];\n[downloadQueue addTask:task2];\n[downloadQueue addTask:task3]; // task3 will be sent after task1 or task2 is finished.\n*/\n```\n\n### Use STNetTaskGroup to execute multiple net tasks\nSTNetTaskGroup supports two modes: STNetTaskGroupModeSerial and STNetTaskGroupConcurrent.\nSTNetTaskGroupModeSerial will execute a net task after the previous net task is finished.\nSTNetTaskGroupModeConcurrent will execute all net tasks concurrently.\n```objc\nSTTestGetNetTask *task1 = [STTestGetNetTask new];\ntask1.id = 1;\n    \nSTTestGetNetTask *task2 = [STTestGetNetTask new];\ntask2.id = 2;\n    \nSTNetTaskGroup *group = [[STNetTaskGroup alloc] initWithTasks:@[ task1, task2 ] mode:STNetTaskGroupModeSerial];\n[group subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {\n    if (error) {\n        // One of the net task is failed.\n        return;\n    }\n    // All net tasks are finished without error.\n}];\n[group start];\n```\n\nOr a handy way:\n```objc\nSTTestGetNetTask *task1 = [STTestGetNetTask new];\ntask1.id = 1;\n    \nSTTestGetNetTask *task2 = [STTestGetNetTask new];\ntask2.id = 2;\n\n[[[@[ task1, task2 ] serialNetTaskGroup] subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {\n    if (error) {\n        // One of the net task is failed.\n        return;\n    }\n    // All net tasks are finished without error.\n}] start];\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevin-lyn%2Fstnettaskqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkevin-lyn%2Fstnettaskqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevin-lyn%2Fstnettaskqueue/lists"}