Skip to content

Commit 7e14460

Browse files
committed
Added an NSRecursiveLock to protect mutable state
1 parent 08c89a7 commit 7e14460

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

NetworkingWingman-Example/Networking/Abstract/Operation/NWMOperation.m

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#import "NWMOperation.h"
1010

11+
@interface NWMOperation ()
12+
13+
@property (nonatomic, strong) NSRecursiveLock *stateLock;
14+
15+
@end
16+
1117
@implementation NWMOperation
1218

1319
@synthesize ready = _ready;
@@ -22,6 +28,7 @@ - (instancetype)init
2228

2329
if (self)
2430
{
31+
self.stateLock = [[NSRecursiveLock alloc] init];
2532
self.ready = YES;
2633
}
2734

@@ -32,56 +39,83 @@ - (instancetype)init
3239

3340
- (void)setReady:(BOOL)ready
3441
{
42+
[self.stateLock lock];
43+
3544
if (_ready != ready)
3645
{
3746
[self willChangeValueForKey:NSStringFromSelector(@selector(isReady))];
3847
_ready = ready;
3948
[self didChangeValueForKey:NSStringFromSelector(@selector(isReady))];
4049
}
50+
51+
[self.stateLock unlock];
4152
}
4253

4354
- (BOOL)isReady
4455
{
45-
return [super isReady] && _ready;
56+
[self.stateLock lock];
57+
BOOL result = [super isReady] && _ready;
58+
[self.stateLock unlock];
59+
60+
return result;
4661
}
4762

4863
- (void)setExecuting:(BOOL)executing
4964
{
65+
[self.stateLock lock];
66+
5067
if (_executing != executing)
5168
{
5269
[self willChangeValueForKey:NSStringFromSelector(@selector(isExecuting))];
5370
_executing = executing;
5471
[self didChangeValueForKey:NSStringFromSelector(@selector(isExecuting))];
5572
}
73+
74+
[self.stateLock unlock];
5675
}
5776

5877
- (BOOL)isExecuting
5978
{
60-
return _executing;
79+
[self.stateLock lock];
80+
BOOL result = _executing;
81+
[self.stateLock unlock];
82+
83+
return result;
6184
}
6285

6386
- (void)setFinished:(BOOL)finished
6487
{
88+
[self.stateLock lock];
89+
6590
if (_finished != finished)
6691
{
6792
[self willChangeValueForKey:NSStringFromSelector(@selector(isFinished))];
6893
_finished = finished;
6994
[self didChangeValueForKey:NSStringFromSelector(@selector(isFinished))];
7095
}
96+
97+
[self.stateLock unlock];
7198
}
7299

73100
- (BOOL)isFinished
74101
{
75-
return _finished;
102+
[self.stateLock lock];
103+
BOOL result = _finished;
104+
[self.stateLock unlock];
105+
106+
return result;
76107
}
77108

78109
#pragma mark - Start
79110

80111
- (void)start
81112
{
113+
[self.stateLock lock];
114+
82115
if (self.isCancelled)
83116
{
84117
[self finish];
118+
[self.stateLock unlock];
85119
return;
86120
}
87121

@@ -91,17 +125,23 @@ - (void)start
91125
self.executing = YES;
92126
self.finished = NO;
93127
}
128+
129+
[self.stateLock unlock];
94130
}
95131

96132
#pragma mark - Finish
97133

98134
- (void)finish
99135
{
136+
[self.stateLock lock];
137+
100138
if (self.executing)
101139
{
102140
self.executing = NO;
103141
self.finished = YES;
104142
}
143+
144+
[self.stateLock unlock];
105145
}
106146

107147
#pragma mark - Cancel

0 commit comments

Comments
 (0)