Skip to content

Commit 9966689

Browse files
authored
Merge pull request #2938 from readdle/unblock-block-operation
Fix BlockOperation with multiple execution blocks
2 parents d24feed + 30ef6af commit 9966689

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Sources/Foundation/Operation.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,22 +668,16 @@ open class BlockOperation : Operation {
668668
}
669669
_lock()
670670
defer { _unlock() }
671-
if _block == nil && _executionBlocks == nil {
671+
if _block == nil {
672672
_block = block
673+
} else if _executionBlocks == nil {
674+
_executionBlocks = [block]
673675
} else {
674-
if _executionBlocks == nil {
675-
if let existing = _block {
676-
_executionBlocks = [existing, block]
677-
} else {
678-
_executionBlocks = [block]
679-
}
680-
} else {
681-
_executionBlocks?.append(block)
682-
}
676+
_executionBlocks?.append(block)
683677
}
684678
}
685679

686-
open var executionBlocks: [@convention(block) () -> Void] {
680+
open var executionBlocks: [() -> Void] {
687681
get {
688682
_lock()
689683
defer { _unlock() }

Tests/Foundation/Tests/TestOperationQueue.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestOperationQueue : XCTestCase {
4242
("test_Lifecycle", test_Lifecycle),
4343
("test_ConcurrentOperations", test_ConcurrentOperations),
4444
("test_ConcurrentOperationsWithDependenciesAndCompletions", test_ConcurrentOperationsWithDependenciesAndCompletions),
45+
("test_BlockOperationAddExecutionBlock", test_BlockOperationAddExecutionBlock),
4546
]
4647
}
4748

@@ -753,6 +754,21 @@ class TestOperationQueue : XCTestCase {
753754
}
754755
}
755756

757+
func test_BlockOperationAddExecutionBlock() {
758+
let block1Expectation = expectation(description: "Block 1 executed")
759+
let block2Expectation = expectation(description: "Block 2 executed")
760+
761+
let blockOperation = BlockOperation {
762+
block1Expectation.fulfill()
763+
}
764+
blockOperation.addExecutionBlock {
765+
block2Expectation.fulfill()
766+
}
767+
XCTAssert(blockOperation.executionBlocks.count == 2)
768+
let queue = OperationQueue()
769+
queue.addOperation(blockOperation)
770+
waitForExpectations(timeout: 1.0)
771+
}
756772
}
757773

758774
class AsyncOperation: Operation {

0 commit comments

Comments
 (0)