Skip to content

Fixbug, BlockOperation addExecutionBlock #2497

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

Closed
Closed
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
18 changes: 6 additions & 12 deletions Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ open class Operation : NSObject {
}
}
}

open func addDependency(_ op: Operation) {
_addDependency(op)
}
Expand Down Expand Up @@ -668,22 +668,16 @@ open class BlockOperation : Operation {
}
_lock()
defer { _unlock() }
if _block == nil && _executionBlocks == nil {
if _block == nil {
_block = block
} else if _executionBlocks == nil {
_executionBlocks = [block]
} else {
if _executionBlocks == nil {
if let existing = _block {
_executionBlocks = [existing, block]
} else {
_executionBlocks = [block]
}
} else {
_executionBlocks?.append(block)
}
_executionBlocks?.append(block)
}
}

open var executionBlocks: [@convention(block) () -> Void] {
open var executionBlocks: [() -> Void] {
get {
_lock()
defer { _unlock() }
Expand Down
16 changes: 16 additions & 0 deletions TestFoundation/TestOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestOperationQueue : XCTestCase {
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
("test_isSuspended", test_isSuspended),
("test_OperationDependencyCount", test_OperationDependencyCount),
("test_BlockOperationAddExecutionBlock", test_BlockOperationAddExecutionBlock)
]
}

Expand Down Expand Up @@ -279,6 +280,21 @@ class TestOperationQueue : XCTestCase {
op1.addDependency(op2)
XCTAssert(op1.dependencies.count == 1)
}

func test_BlockOperationAddExecutionBlock() {
var msgOperations = [String]()
let blockOperation = BlockOperation {
msgOperations.append("block1 executed")
}
blockOperation.addExecutionBlock {
msgOperations.append("block2 executec")
}
XCTAssert(blockOperation.executionBlocks.count == 2)
let queue = OperationQueue()
queue.addOperation(blockOperation)
queue.waitUntilAllOperationsAreFinished()
XCTAssertEqual(msgOperations, ["block1 executed", "block2 executec"])
}
}

class AsyncOperation: Operation {
Expand Down