From ad9e2960e1a3dd07517d476916174235fc896177 Mon Sep 17 00:00:00 2001 From: kinarob Date: Fri, 21 Jun 2019 16:24:56 +0800 Subject: [PATCH 1/3] delete unused flag in operation group --- Source/PINOperationGroup.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Source/PINOperationGroup.m b/Source/PINOperationGroup.m index ddd2ed0..706c1ca 100644 --- a/Source/PINOperationGroup.m +++ b/Source/PINOperationGroup.m @@ -28,7 +28,6 @@ @interface PINOperationGroup () dispatch_block_t _completion; - BOOL _started; BOOL _canceled; } @@ -75,7 +74,7 @@ - (void)start { [self lock]; NSAssert(_canceled == NO, @"Operation group canceled."); - if (_started == NO && _canceled == NO) { + if (_canceled == NO) { for (NSUInteger idx = 0; idx < _operations.count; idx++) { dispatch_group_enter(_group); dispatch_block_t originalOperation = _operations[idx]; @@ -132,8 +131,8 @@ - (void)cancel { [self lock]; id reference = nil; - NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled."); - if (_started == NO && _canceled == NO) { + NSAssert(_canceled == NO, @"Operation group already canceled."); + if (_canceled == NO) { reference = [self locked_nextOperationReference]; [_operations addObject:operation]; [_operationPriorities addObject:@(priority)]; @@ -147,8 +146,8 @@ - (void)cancel - (void)setCompletion:(dispatch_block_t)completion { [self lock]; - NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled."); - if (_started == NO && _canceled == NO) { + NSAssert(_canceled == NO, @"Operation group already canceled."); + if (_canceled == NO) { _completion = completion; } [self unlock]; From c49b7a50e9fee09c3f8699b2192c0763db2a29a8 Mon Sep 17 00:00:00 2001 From: kinarob Date: Sat, 22 Jun 2019 13:28:01 +0800 Subject: [PATCH 2/3] Fix group can add operations and set completion multiple times when the group start --- CHANGELOG.md | 2 ++ Source/PINOperationGroup.m | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd7124..3ec843f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## master * Add your own contributions to the next release on the line below this with your name. +- [fix] Fix group can add operations and set completion multiple times when the group start. [#22](https://github.com/pinterest/PINOperation/pull/22) + ## 1.1.1 -- 2018 February 3 * Cleanup warnings [garrett](https://github.com/garrettmoon) diff --git a/Source/PINOperationGroup.m b/Source/PINOperationGroup.m index 706c1ca..ee877ee 100644 --- a/Source/PINOperationGroup.m +++ b/Source/PINOperationGroup.m @@ -28,6 +28,7 @@ @interface PINOperationGroup () dispatch_block_t _completion; + BOOL _started; BOOL _canceled; } @@ -74,7 +75,8 @@ - (void)start { [self lock]; NSAssert(_canceled == NO, @"Operation group canceled."); - if (_canceled == NO) { + if (_started == NO && _canceled == NO) { + _started = YES; for (NSUInteger idx = 0; idx < _operations.count; idx++) { dispatch_group_enter(_group); dispatch_block_t originalOperation = _operations[idx]; @@ -131,8 +133,8 @@ - (void)cancel { [self lock]; id reference = nil; - NSAssert(_canceled == NO, @"Operation group already canceled."); - if (_canceled == NO) { + NSAssert(_started == NO && _canceled == NO, @"Operation group already canceled."); + if (_started == NO && _canceled == NO) { reference = [self locked_nextOperationReference]; [_operations addObject:operation]; [_operationPriorities addObject:@(priority)]; @@ -146,8 +148,8 @@ - (void)cancel - (void)setCompletion:(dispatch_block_t)completion { [self lock]; - NSAssert(_canceled == NO, @"Operation group already canceled."); - if (_canceled == NO) { + NSAssert(_started == NO && _canceled == NO, @"Operation group already canceled."); + if (_started == NO && _canceled == NO) { _completion = completion; } [self unlock]; @@ -166,6 +168,7 @@ - (void)runCompletionIfNeeded [self lock]; completion = _completion; _completion = nil; + _started = NO; [self unlock]; if (completion) { From 4a3c186792f309b9b5043557c9a2857d4a55c8cb Mon Sep 17 00:00:00 2001 From: kinarob Date: Sat, 22 Jun 2019 13:29:25 +0800 Subject: [PATCH 3/3] typo --- Source/PINOperationGroup.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/PINOperationGroup.m b/Source/PINOperationGroup.m index ee877ee..98539e9 100644 --- a/Source/PINOperationGroup.m +++ b/Source/PINOperationGroup.m @@ -133,7 +133,7 @@ - (void)cancel { [self lock]; id reference = nil; - NSAssert(_started == NO && _canceled == NO, @"Operation group already canceled."); + NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled."); if (_started == NO && _canceled == NO) { reference = [self locked_nextOperationReference]; [_operations addObject:operation]; @@ -148,7 +148,7 @@ - (void)cancel - (void)setCompletion:(dispatch_block_t)completion { [self lock]; - NSAssert(_started == NO && _canceled == NO, @"Operation group already canceled."); + NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled."); if (_started == NO && _canceled == NO) { _completion = completion; }