Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made AsyncOperation accessors are thread-safe and fixed project tests #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ - (void)testThatChainaleOperationBaseCallsInputDataClassMethodWhenStarted {
}];

// when
[self.chainableOperationBaseMock start];
[(ChainableOperationBase *)self.chainableOperationBaseMock start];

// then
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
Expand All @@ -135,7 +135,7 @@ - (void)testThatChainaleOperationBaseCallsProcessDataMethodWhenStarted {
}];

// when
[self.chainableOperationBaseMock start];
[(ChainableOperationBase *)self.chainableOperationBaseMock start];

// then
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
Expand All @@ -152,7 +152,7 @@ - (void)testThatChainaleOperationBaseCallsProcessDataMethodWithCorrectParameters
}];

// when
[self.chainableOperationBaseMock start];
[(ChainableOperationBase *)self.chainableOperationBaseMock start];

// then
__weak __typeof__(self) weakSelf = self;
Expand All @@ -172,7 +172,7 @@ - (void)testThatChainaleOperationBaseMakeCorrectOutputAfterDataProcessing {
}];

// when
[self.chainableOperationBaseMock start];
[(ChainableOperationBase *)self.chainableOperationBaseMock start];

// then
__weak __typeof__(self) weakSelf = self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ - (void)testThatCompoundOperationCancellsCorrectly {
// then
OCMVerify([self.mockOperationQueue setSuspended:YES]);
OCMVerify([self.mockOperationQueue cancelAllOperations]);
OCMVerify([self.mockOperationQueue setSuspended:NO]);
}


Expand Down
82 changes: 64 additions & 18 deletions Source/Base/AsyncOperation/AsyncOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@

#import "AsyncOperation.h"

static NSString *const kExecutingFlagSelector = @"isExecuting";
static NSString *const kFinishedFlagSelector = @"isFinished";
@interface AsyncOperation ()

@property (strong, nonatomic) NSRecursiveLock *recursiveLock;

@end

@implementation AsyncOperation {
BOOL executing;
BOOL finished;
};
}

- (instancetype)init {
self = [super init];
if (self) {
executing = NO;
finished = NO;
_recursiveLock = [[NSRecursiveLock alloc] init];
_recursiveLock.name = [NSString stringWithFormat:@"com.strongself.%@-lock", [self class]];
}
return self;
}
Expand All @@ -44,14 +49,22 @@ - (BOOL)isAsynchronous {
}

- (BOOL)isExecuting {
return executing;
[self.recursiveLock lock];
BOOL result = executing;
[self.recursiveLock unlock];

return result;
}

- (BOOL)isFinished {
return finished;
[self.recursiveLock lock];
BOOL result = finished;
[self.recursiveLock unlock];

return result;
}

#pragma mark - Private methods
#pragma mark - NSOperation overrides

- (void)start {
/**
Expand All @@ -73,11 +86,8 @@ - (void)start {

If it wasn't cancelled and wasn't started manually, we're beginning the task
*/
[self willChangeValueForKey:kExecutingFlagSelector];

[self lockedMarkStarted];
[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
executing = YES;
[self didChangeValueForKey:kExecutingFlagSelector];
}
}

Expand All @@ -86,20 +96,56 @@ - (void)main {
format:@"You should override the method %@ in a subclass", NSStringFromSelector(_cmd)];
}

#pragma mark - Utils

- (void)changeValueForKey:(NSString *)key inBlock:(void(^)())block {
[self willChangeValueForKey:key];
block();
[self didChangeValueForKey:key];
}

- (void)lock:(void(^)())block {
[self.recursiveLock lock];
block();
[self.recursiveLock unlock];
}

#pragma mark - State management

- (void)markFinished {
[self changeValueForKey:NSStringFromSelector(@selector(isFinished)) inBlock:^{
finished = YES;
}];
}

- (void)markStarted {
[self changeValueForKey:NSStringFromSelector(@selector(isExecuting)) inBlock:^{
executing = YES;
}];
}

- (void)lockedMarkStarted {
[self lock:^{
[self markStarted];
}];
}

- (void)markComplete {
[self changeValueForKey:NSStringFromSelector(@selector(isExecuting)) inBlock:^{
executing = NO;
}];
}

- (void)complete {
/**
@author Egor Tolstoy

We should always manually setup finished and executing flags after the operation is complete or cancelled
*/
[self willChangeValueForKey:kFinishedFlagSelector];
[self willChangeValueForKey:kExecutingFlagSelector];

executing = NO;
finished = YES;

[self didChangeValueForKey:kExecutingFlagSelector];
[self didChangeValueForKey:kFinishedFlagSelector];
[self lock:^{
[self markComplete];
[self markFinished];
}];
}

@end
8 changes: 2 additions & 6 deletions Source/Base/CompoundOperation/CompoundOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,11 @@ - (void)main {
- (void)cancel {
// We should cancel the operation only if it's executing
if (![self isFinished] && ![self isCancelled]) {
[super cancel];

if ([self isExecuting]) {
[self finishCompoundOperationExecution];
}
[super cancel];
[self finishCompoundOperationExecution];
}
}


#pragma mark - <ChainableOperationDelegate>

- (void)didCompleteChainableOperationWithError:(NSError *)error {
Expand Down