The iOS framework that grows only as fast as its documentation
NIOperations.m
1 //
2 // Copyright 2011-2014 NimbusKit
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #import "NIOperations.h"
18 
19 #import "NIDebuggingTools.h"
20 #import "NIPreprocessorMacros.h"
21 #import "NIOperations+Subclassing.h"
22 
23 #if !defined(__has_feature) || !__has_feature(objc_arc)
24 #error "Nimbus requires ARC support."
25 #endif
26 
27 @implementation NIOperation
28 
29 - (void)dealloc {
30  // For an unknown reason these block objects are not released when the NIOperation is deallocated
31  // with ARC enabled.
32  _didStartBlock = nil;
33  _didFinishBlock = nil;
34  _didFailWithErrorBlock = nil;
35  _willFinishBlock = nil;
36 }
37 
38 #pragma mark - Initiate delegate notification from the NSOperation
39 
40 - (void)didStart {
41  [self performSelectorOnMainThread:@selector(onMainThreadOperationDidStart)
42  withObject:nil
43  waitUntilDone:[NSThread isMainThread]];
44 }
45 
46 - (void)didFinish {
47  [self performSelectorOnMainThread:@selector(onMainThreadOperationDidFinish)
48  withObject:nil
49  waitUntilDone:[NSThread isMainThread]];
50 }
51 
52 - (void)didFailWithError:(NSError *)error {
53  self.lastError = error;
54 
55  [self performSelectorOnMainThread:@selector(onMainThreadOperationDidFailWithError:)
56  withObject:error
57  waitUntilDone:[NSThread isMainThread]];
58 }
59 
60 - (void)willFinish {
61  if ([self.delegate respondsToSelector:@selector(nimbusOperationWillFinish:)]) {
62  [self.delegate nimbusOperationWillFinish:self];
63  }
64 
65  if (nil != self.willFinishBlock) {
66  self.willFinishBlock(self);
67  }
68 }
69 
70 #pragma mark - Main Thread
71 
72 - (void)onMainThreadOperationDidStart {
73  // This method should only be called on the main thread.
74  NIDASSERT([NSThread isMainThread]);
75 
76  if ([self.delegate respondsToSelector:@selector(nimbusOperationDidStart:)]) {
77  [self.delegate nimbusOperationDidStart:self];
78  }
79 
80  if (nil != self.didStartBlock) {
81  self.didStartBlock(self);
82  }
83 }
84 
85 - (void)onMainThreadOperationDidFinish {
86  // This method should only be called on the main thread.
87  NIDASSERT([NSThread isMainThread]);
88 
89  if ([self.delegate respondsToSelector:@selector(nimbusOperationDidFinish:)]) {
90  [self.delegate nimbusOperationDidFinish:self];
91  }
92 
93  if (nil != self.didFinishBlock) {
94  self.didFinishBlock(self);
95  }
96 }
97 
98 - (void)onMainThreadOperationDidFailWithError:(NSError *)error {
99  // This method should only be called on the main thread.
100  NIDASSERT([NSThread isMainThread]);
101 
102  if ([self.delegate respondsToSelector:@selector(nimbusOperationDidFail:withError:)]) {
103  [self.delegate nimbusOperationDidFail:self withError:error];
104  }
105 
106  if (nil != self.didFailWithErrorBlock) {
107  self.didFailWithErrorBlock(self, error);
108  }
109 }
110 
111 @end
NIOperationDidFailBlock didFailWithErrorBlock
The operation failed in some way and has completed.
Definition: NIOperations.h:59
void willFinish()
In the operation's thread, notify the delegate that the operation will finish successfully.
Definition: NIOperations.m:60
NIOperationBlock didStartBlock
The operation has started executing.
Definition: NIOperations.h:57
void didFinish()
On the main thread, notify the delegate that the operation has finished.
Definition: NIOperations.m:46
NIOperationBlock willFinishBlock
The operation is about to complete successfully.
Definition: NIOperations.h:60
A base implementation of an NSOperation that supports traditional delegation and blocks.
Definition: NIOperations.h:51
id< NIOperationDelegate > delegate
The delegate through which changes are notified for this operation.
Definition: NIOperations.h:53
void didStart()
On the main thread, notify the delegate that the operation has begun.
Definition: NIOperations.m:40
NIOperationBlock didFinishBlock
The operation has completed successfully.
Definition: NIOperations.h:58