-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSTNetTask.h
More file actions
144 lines (109 loc) · 3.6 KB
/
STNetTask.h
File metadata and controls
144 lines (109 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// STNetTask.h
// STNetTaskQueue
//
// Created by Kevin Lin on 29/11/14.
// Copyright (c) 2014 Sth4Me. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXPORT NSString *const STNetTaskUnknownError;
#ifdef RACObserve
#define STNetTaskObserve(TASK) \
[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { \
[[[[RACObserve(TASK, finished) skip:1] ignore:@NO] deliverOnMainThread] subscribeNext:^(id x) { \
[subscriber sendNext:TASK];\
[subscriber sendCompleted]; \
}]; \
[[[[RACObserve(TASK, cancelled) skip:1] ignore:@NO] deliverOnMainThread] subscribeNext:^(id x) { \
[subscriber sendError:nil];\
}]; \
return nil; \
}]
#endif
@class STNetTask;
@protocol STNetTaskDelegate <NSObject>
/**
This delegate method will be called when the net task is finished(no matter it's successful or failed).
If the net task is failed, task.error will be non-nil.
@param task STNetTask The finished net task.
*/
- (void)netTaskDidEnd:(__kindof STNetTask *)task;
@end
typedef NS_ENUM(NSUInteger, STNetTaskState) {
STNetTaskStateCancalled,
STNetTaskStateFinished,
STNetTaskStateRetrying
};
typedef void (^STNetTaskSubscriptionBlock)();
@interface STNetTask : NSObject
/**
Error object which contains error message when net task is failed.
*/
@property (nullable, atomic, strong) NSError *error;
/**
Indicates if the net task is waiting for executing or executing.
This value will be set to "YES" immediately after the net task is added to net task queue.
*/
@property (atomic, assign, readonly) BOOL pending;
/**
Indicates if the net task is cancelled.
This value would be "NO" by default after net task is created, even the net task is not added to queue.
*/
@property (atomic, assign, readonly) BOOL cancelled;
/**
Indicates if the net task is finished(no matter it's successful or failed).
*/
@property (atomic, assign, readonly) BOOL finished;
/**
The current retry time @see maxRetryCount
*/
@property (atomic, assign, readonly) NSUInteger retryCount;
/**
A unique string represents the net task.
@return NSString The uri string.
*/
- (NSString *)uri;
/**
A callback method which is called when the net task is finished successfully.
Note: this method will be called in thread of STNetTaskQueue.
@param response id The response object.
*/
- (void)didResponse:(id)response;
/**
A callback method which is called when the net task is failed.
Note: this method will be called in thread of STNetTaskQueue.
*/
- (void)didFail;
/**
A callback method which is called when the net task is retried.
Note: this method will be called in thread of STNetTaskQueue.
*/
- (void)didRetry;
/**
Indicates how many times the net task should be retried after failed.
Default 0.
@return NSUInteger
*/
- (NSUInteger)maxRetryCount;
/**
If you are going to retry the net task only when specific error is returned, return NO in this method.
Default YES.
@param error NSError Error object.
@return BOOL Should the net task be retried according to the error object.
*/
- (BOOL)shouldRetryForError:(NSError *)error;
/**
Indicates how many seconds should be delayed before retrying the net task.
@return NSTimeInterval
*/
- (NSTimeInterval)retryInterval;
/**
Subscribe state of net task by using block
@param state STNetTaskState state of net task
@param block STNetTaskSubscriptionBlock block is called when net task is in subscribed state.
NOTE: this block will be called in main thread.
*/
- (void)subscribeState:(STNetTaskState)state usingBlock:(STNetTaskSubscriptionBlock)block;
@end
NS_ASSUME_NONNULL_END