Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (87 sloc) 3.263 kb
ef21de8 @jstn initial commit
authored
1 #import "JXOperation.h"
2
3 @interface JXOperation ()
4 @property (assign) BOOL isExecuting;
5 @property (assign) BOOL isFinished;
9dcd280 @jstn continuesInAppBackground
authored
6 @property (assign) UIBackgroundTaskIdentifier backgroundTaskID;
ef21de8 @jstn initial commit
authored
7 @end
8
9 @implementation JXOperation
10
9dcd280 @jstn continuesInAppBackground
authored
11 @synthesize isExecuting, isFinished, startsOnMainThread, continuesInAppBackground, backgroundTaskID;
ef21de8 @jstn initial commit
authored
12
13 #pragma mark -
14 #pragma mark Initialization
15
9dcd280 @jstn continuesInAppBackground
authored
16 - (void)dealloc
17 {
18 [self removeObserver:self forKeyPath:@"continuesInAppBackground"];
19 [self removeObserver:self forKeyPath:@"isFinished"];
20
21 if (self.backgroundTaskID != UIBackgroundTaskInvalid)
22 [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
23
24 [super dealloc];
25 }
26
ef21de8 @jstn initial commit
authored
27 - (id)init
28 {
29 if ((self = [super init])) {
30 self.isExecuting = NO;
31 self.isFinished = NO;
9dcd280 @jstn continuesInAppBackground
authored
32 self.startsOnMainThread = NO;
33 self.continuesInAppBackground = NO;
34 self.backgroundTaskID = UIBackgroundTaskInvalid;
35
36 [self addObserver:self forKeyPath:@"continuesInAppBackground" options:0 context:NULL];
37 [self addObserver:self forKeyPath:@"isFinished" options:0 context:NULL];
ef21de8 @jstn initial commit
authored
38 }
39 return self;
40 }
41
42 #pragma mark -
43 #pragma mark NSOperation
44
45 - (void)start
46 {
9dcd280 @jstn continuesInAppBackground
authored
47 if (self.startsOnMainThread && ![NSThread isMainThread]) {
ef21de8 @jstn initial commit
authored
48 [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
49 return;
50 }
51
52 [self willChangeValueForKey:@"isExecuting"];
53 self.isExecuting = YES;
54 [self didChangeValueForKey:@"isExecuting"];
12d31ad @jstn finer-grained cancellation
authored
55
56 if (self.isCancelled) {
57 [self finish];
58 } else {
59 @autoreleasepool {
60 [self main];
61 }
ef21de8 @jstn initial commit
authored
62 }
63 }
64
65 - (void)main
66 {
9dcd280 @jstn continuesInAppBackground
authored
67 NSAssert(NO, @"subclasses must implement and eventually call finish");
ef21de8 @jstn initial commit
authored
68 }
69
70 - (void)finish
71 {
72 [self willChangeValueForKey:@"isExecuting"];
73 [self willChangeValueForKey:@"isFinished"];
74
75 self.isExecuting = NO;
76 self.isFinished = YES;
77
78 [self didChangeValueForKey:@"isExecuting"];
79 [self didChangeValueForKey:@"isFinished"];
80 }
81
9dcd280 @jstn continuesInAppBackground
authored
82 #pragma mark -
83 #pragma mark <NSKeyValueObserving>
84
85 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
86 {
87 if (object == self && [keyPath isEqualToString:@"continuesInAppBackground"]) {
88 if (self.continuesInAppBackground && !self.isCancelled) {
89 UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid;
90 taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
91 [[UIApplication sharedApplication] endBackgroundTask:taskID];
92 }];
93 self.backgroundTaskID = taskID;
94 } else if (!self.continuesInAppBackground && self.backgroundTaskID != UIBackgroundTaskInvalid) {
95 [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
96 self.backgroundTaskID = UIBackgroundTaskInvalid;
97 }
98 }
99
079f17a @jstn check if actually finished
authored
100 if (object == self && [keyPath isEqualToString:@"isFinished"]) {
101 if (self.isFinished && self.backgroundTaskID != UIBackgroundTaskInvalid) {
102 [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
103 self.backgroundTaskID = UIBackgroundTaskInvalid;
104 }
9dcd280 @jstn continuesInAppBackground
authored
105 }
106 }
107
ef21de8 @jstn initial commit
authored
108 @end
Something went wrong with that request. Please try again.