-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIOperations.h
304 lines (241 loc) · 6.89 KB
/
NIOperations.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "NIBlocks.h"
/**
* For writing code that runs concurrently.
*
* @ingroup NimbusCore
* @defgroup Operations Operations
*
* This collection of NSOperation implementations is meant to provide a set of common
* operations that might be used in an application to offload complex processing to a separate
* thread.
*/
@protocol NIOperationDelegate;
/**
* A base implementation of an NSOperation that supports traditional delegation and blocks.
*
*
* <h2>Subclassing</h2>
*
* A subclass should call the operationDid* methods to notify the delegate on the main thread
* of changes in the operation's state. Calling these methods will notify the delegate and the
* blocks if provided.
*
* @ingroup Operations
*/
@interface NIOperation : NSOperation {
@private
id<NIOperationDelegate> _delegate;
NSInteger _tag;
NSError* _lastError;
#if NS_BLOCKS_AVAILABLE
// Performed on the main thread.
NIBasicBlock _didStartBlock;
NIBasicBlock _didFinishBlock;
NIErrorBlock _didFailWithErrorBlock;
// Performed in the operation's thread.
NIBasicBlock _willFinishBlock;
#endif // #if NS_BLOCKS_AVAILABLE
}
@property (readwrite, assign) id<NIOperationDelegate> delegate;
@property (readonly, retain) NSError* lastError;
@property (readwrite, assign) NSInteger tag;
#if NS_BLOCKS_AVAILABLE
@property (readwrite, copy) NIBasicBlock didStartBlock;
@property (readwrite, copy) NIBasicBlock didFinishBlock;
@property (readwrite, copy) NIErrorBlock didFailWithErrorBlock;
@property (readwrite, copy) NIBasicBlock willFinishBlock;
#endif // #if NS_BLOCKS_AVAILABLE
- (void)operationDidStart;
- (void)operationDidFinish;
- (void)operationDidFailWithError:(NSError *)error;
- (void)operationWillFinish;
@end
/**
* An operation that reads a file from disk.
*
* Provides asynchronous file reading support when added to an NSOperationQueue.
*
* It is recommended to add this operation to a serial NSOperationQueue to avoid overlapping
* disk read attempts. This will noticeably improve performance when loading many files
* from disk at once.
*
* @ingroup Operations
*/
@interface NIReadFileFromDiskOperation : NIOperation {
@private
// [in]
NSString* _pathToFile;
// [out]
NSData* _data;
id _processedObject;
}
// Designated initializer.
- (id)initWithPathToFile:(NSString *)pathToFile;
@property (readwrite, copy) NSString* pathToFile;
@property (readonly, retain) NSData* data;
@property (readwrite, retain) id processedObject;
@end
/**
* The delegate protocol for an NSOperation.
*
* @ingroup Operations
*/
@protocol NIOperationDelegate <NSObject>
@optional
/** @name [NIOperationDelegate] State Changes */
/** The operation has started executing. */
- (void)operationDidStart:(NSOperation *)operation;
/**
* The operation is about to complete successfully.
*
* This will not be called if the operation fails.
*
* This will be called from within the operation's runloop and must be thread safe.
*/
- (void)operationWillFinish:(NSOperation *)operation;
/**
* The operation has completed successfully.
*
* This will not be called if the operation fails.
*/
- (void)operationDidFinish:(NSOperation *)operation;
/**
* The operation failed in some way and has completed.
*
* operationDidFinish: will not be called.
*/
- (void)operationDidFail:(NSOperation *)operation withError:(NSError *)error;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
// NIOperation
/** @name Delegation */
/**
* The delegate through which changes are notified for this operation.
*
* All delegate methods are performed on the main thread.
*
* @fn NIOperation::delegate
*/
/** @name Post-Operation Properties */
/**
* The error last passed to the didFailWithError notification.
*
* @fn NIOperation::lastError
*/
/** @name Identification */
/**
* A simple tagging mechanism for identifying operations.
*
* @fn NIOperation::tag
*/
#if NS_BLOCKS_AVAILABLE
/** @name Blocks */
/**
* The operation has started executing.
*
* Performed on the main thread.
*
* @fn NIOperation::didStartBlock
*/
/**
* The operation has completed successfully.
*
* This will not be called if the operation fails.
*
* Performed on the main thread.
*
* @fn NIOperation::didFinishBlock
*/
/**
* The operation failed in some way and has completed.
*
* didFinishBlock will not be executed.
*
* Performed on the main thread.
*
* @fn NIOperation::didFailWithErrorBlock
*/
/**
* The operation is about to complete successfully.
*
* This will not be called if the operation fails.
*
* Performed in the operation's thread.
*
* @fn NIOperation::willFinishBlock
*/
#endif // #if NS_BLOCKS_AVAILABLE
/**
* @name Subclassing
*
* The following methods are provided to aid in subclassing and are not meant to be
* used externally.
*/
/**
* On the main thread, notify the delegate that the operation has begun.
*
* @fn NIOperation::operationDidStart
*/
/**
* On the main thread, notify the delegate that the operation has finished.
*
* @fn NIOperation::operationDidFinish
*/
/**
* On the main thread, notify the delegate that the operation has failed.
*
* @fn NIOperation::operationDidFailWithError:
*/
/**
* In the operation's thread, notify the delegate that the operation will finish successfully.
*
* @fn NIOperation::operationWillFinish
*/
///////////////////////////////////////////////////////////////////////////////////////////////////
// NIReadFileFromDiskOperation
/** @name Creating an Operation */
/**
* Initializes a newly allocated "read from disk" operation with a given path to a file to be read.
*
* @fn NIReadFileFromDiskOperation::initWithPathToFile:
*/
/** @name Configuring the Operation */
/**
* The path to the file that should be read from disk.
*
* @fn NIReadFileFromDiskOperation::pathToFile
*/
/** @name Operation Results */
/**
* The data that was read from disk.
*
* Will be nil if the data couldn't be read.
*
* @sa NIOperation::lastError
* @fn NIReadFileFromDiskOperation::data
*/
/**
* An object created from the data that was read from disk.
*
* Will be nil if the data couldn't be read.
*
* @sa NIOperation::lastError
* @fn NIReadFileFromDiskOperation::processedObject
*/