GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Initial import. · kevin-lyn/STNetTaskQueue@63a711f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • STNetTaskQueueExample.xcodeproj
    • project.pbxproj
    • project.xcworkspace
      • contents.xcworkspacedata
  • STNetTaskQueueExample
    • AFNetworking
      • AFHTTPRequestOperation.h
      • AFHTTPRequestOperation.m
      • AFHTTPRequestOperationManager.h
      • AFHTTPRequestOperationManager.m
      • AFHTTPSessionManager.h
      • AFHTTPSessionManager.m
      • AFNetworkReachabilityManager.h
      • AFNetworkReachabilityManager.m
      • AFNetworking.h
      • AFSecurityPolicy.h
      • AFSecurityPolicy.m
      • AFURLConnectionOperation.h
      • AFURLConnectionOperation.m
      • AFURLRequestSerialization.h
      • AFURLRequestSerialization.m
      • AFURLResponseSerialization.h
      • AFURLResponseSerialization.m
      • AFURLSessionManager.h
      • AFURLSessionManager.m
    • AppDelegate.h
    • AppDelegate.m
    • Info.plist
    • STOpenWeatherNetTask.h
    • STOpenWeatherNetTask.m
    • ViewController.h
    • ViewController.m
    • main.m
  • STNetTaskQueue
    • STHTTPNetTask.h
    • STHTTPNetTask.m
    • STHTTPNetTaskQueueHandler.h
    • STHTTPNetTaskQueueHandler.m
    • STNetTask.h
    • STNetTask.m
    • STNetTaskChain.h
    • STNetTaskChain.m
    • STNetTaskQueue.h
    • STNetTaskQueue.m
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboard
+89Lines changed: 89 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,91 @@
11
# STNetTaskQueue
22
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+
Collapse file

‎STNetTaskQueue/STHTTPNetTask.h‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// STHTTPNetTask.h
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import "STNetTask.h"
10+
11+
typedef enum {
12+
STHTTPNetTaskGet,
13+
STHTTPNetTaskPost
14+
} STHTTPNetTaskMethod;
15+
16+
@interface STHTTPNetTask : STNetTask
17+
18+
- (STHTTPNetTaskMethod)method;
19+
- (NSDictionary *)parameters;
20+
- (NSDictionary *)datas;
21+
- (void)didResponseJSON:(NSDictionary *)response;
22+
23+
@end
Collapse file

‎STNetTaskQueue/STHTTPNetTask.m‎

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// STHTTPNetTask.m
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import "STHTTPNetTask.h"
10+
11+
@implementation STHTTPNetTask
12+
13+
- (STHTTPNetTaskMethod)method
14+
{
15+
return STHTTPNetTaskGet;
16+
}
17+
18+
- (NSDictionary *)datas
19+
{
20+
return nil;
21+
}
22+
23+
- (NSDictionary *)parameters
24+
{
25+
return nil;
26+
}
27+
28+
- (void)didResponse:(NSObject *)response
29+
{
30+
[self didResponseJSON:(NSDictionary *)response];
31+
}
32+
33+
- (void)didResponseJSON:(NSDictionary *)response
34+
{
35+
36+
}
37+
38+
@end
Collapse file

‎STNetTaskQueue/STHTTPNetTaskQueueHandler.h‎

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// STHTTPNetTaskQueueHandler.h
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "STNetTaskQueue.h"
11+
12+
@interface STHTTPNetTaskQueueHandler : NSObject<STNetTaskQueueHandler>
13+
14+
- (instancetype)initWithQueue:(STNetTaskQueue *)queue baseURL:(NSURL *)baseURL;
15+
16+
@end
Collapse file

‎STNetTaskQueue/STHTTPNetTaskQueueHandler.m‎

Copy file name to clipboard
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// STHTTPNetTaskQueueHandler.m
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import "STHTTPNetTaskQueueHandler.h"
10+
#import "STHTTPNetTask.h"
11+
#import "AFNetworking.h"
12+
13+
@implementation STHTTPNetTaskQueueHandler
14+
{
15+
AFHTTPSessionManager *_httpManager;
16+
STNetTaskQueue *_queue;
17+
}
18+
19+
- (instancetype)initWithQueue:(STNetTaskQueue *)queue baseURL:(NSURL *)baseURL
20+
{
21+
if (self = [super init]) {
22+
_queue = queue;
23+
_httpManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
24+
}
25+
return self;
26+
}
27+
28+
- (void)netTaskQueue:(STNetTaskQueue *)netTaskQueue task:(STNetTask *)task taskId:(int)taskId
29+
{
30+
NSAssert([task isKindOfClass:[STHTTPNetTask class]], @"Should be subclass of STHTTPNetTask");
31+
32+
void (^success)(NSURLSessionDataTask *, id) = ^(NSURLSessionDataTask *task, id responseObject) {
33+
[_queue didResponse:responseObject taskId:taskId];
34+
};
35+
36+
void (^failure)(NSURLSessionDataTask *, NSError *) = ^(NSURLSessionDataTask *task, NSError *error) {
37+
[_queue didFailWithError:error taskId:taskId];
38+
};
39+
40+
STHTTPNetTask *httpTask = (STHTTPNetTask *)task;
41+
NSDictionary *parameters = [httpTask parameters];
42+
43+
if ([httpTask method] == STHTTPNetTaskGet) {
44+
[_httpManager GET:[httpTask uri] parameters:parameters success:success failure:failure];
45+
}
46+
else if ([httpTask method] == STHTTPNetTaskPost) {
47+
NSDictionary *datas = [httpTask datas];
48+
if (!datas.count) {
49+
[_httpManager POST:[httpTask uri] parameters:parameters success:success failure:failure];
50+
}
51+
else {
52+
[_httpManager POST:[httpTask uri] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
53+
for (NSString *name in datas) {
54+
[formData appendPartWithFileData:datas[name] name:name fileName:@"st_file" mimeType:@"*/*"];
55+
}
56+
} success:success failure:failure];
57+
}
58+
}
59+
else {
60+
NSAssert(NO, @"Invalid STHTTPNetTaskMethod");
61+
}
62+
}
63+
64+
@end
Collapse file

‎STNetTaskQueue/STNetTask.h‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// STNetTask.h
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface STNetTask : NSObject
12+
13+
@property (nonatomic, strong) NSError *error;
14+
@property (nonatomic, assign) BOOL pending;
15+
16+
- (NSString *)uri;
17+
- (void)didResponse:(NSObject *)response;
18+
- (void)didFail;
19+
20+
@end
Collapse file

‎STNetTaskQueue/STNetTask.m‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// STNetTask.m
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import "STNetTask.h"
10+
11+
@implementation STNetTask
12+
13+
- (NSString *)uri
14+
{
15+
return @"";
16+
}
17+
18+
- (void)didResponse:(NSObject *)response
19+
{
20+
21+
}
22+
23+
- (void)didFail
24+
{
25+
26+
}
27+
28+
@end
Collapse file

‎STNetTaskQueue/STNetTaskChain.h‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// STNetTaskChain.h
3+
// Sth4Me
4+
//
5+
// Created by Kevin Lin on 29/11/14.
6+
// Copyright (c) 2014 Sth4Me. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "STNetTaskQueue.h"
11+
12+
@class STNetTaskChain;
13+
14+
@protocol STNetTaskChainDelegate <NSObject>
15+
16+
- (void)netTaskChainDidEnd:(STNetTaskChain *)netTaskChain;
17+
18+
@end
19+
20+
@interface STNetTaskChain : NSObject
21+
22+
@property (nonatomic, weak) id<STNetTaskChainDelegate> delegate;
23+
@property (nonatomic, strong) STNetTaskQueue *queue;
24+
@property (nonatomic, strong, readonly) NSError *error;
25+
@property (nonatomic, assign, readonly) BOOL started;
26+
27+
- (void)setTasks:(STNetTask *)task, ...;
28+
// Return NO indicates this task should not be sent.
29+
- (BOOL)onNextRequest:(STNetTask *)task;
30+
- (void)onNextResponse:(STNetTask *)task;
31+
- (void)start;
32+
- (void)cancel;
33+
34+
@end

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.