Skip to content

Commit c0f0181

Browse files
committed
Fix Operation.swift Operation addDependency bug
1 parent 17e0499 commit c0f0181

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Foundation/Operation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ open class Operation : NSObject {
407407
withExtendedLifetime(op) {
408408
var up: Operation?
409409
_lock()
410-
if __dependencies.first(where: { $0 === op }) != nil {
410+
if __dependencies.first(where: { $0 === op }) == nil {
411411
__dependencies.append(op)
412412
up = op
413413
}

TestFoundation/TestOperationQueue.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestOperationQueue : XCTestCase {
2525
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
2626
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
2727
("test_isSuspended", test_isSuspended),
28+
("test_OperationDependencies", test_OperationDependencies),
2829
]
2930
}
3031

@@ -264,6 +265,26 @@ class TestOperationQueue : XCTestCase {
264265

265266
waitForExpectations(timeout: 1)
266267
}
268+
269+
func test_OperationDependencies() {
270+
let queue = OperationQueue()
271+
var results = [Int]()
272+
queue.maxConcurrentOperationCount = 1
273+
let op1 = BlockOperation {
274+
results.append(1)
275+
}
276+
op1.name = "op1"
277+
let op2 = BlockOperation {
278+
results.append(2)
279+
}
280+
op2.name = "op2"
281+
op1.addDependency(op2)
282+
XCTAssert(op1.dependencies.count == 1)
283+
queue.addOperation(op1)
284+
queue.addOperation(op2)
285+
queue.waitUntilAllOperationsAreFinished()
286+
XCTAssertEqual(results, [2, 1])
287+
}
267288
}
268289

269290
class AsyncOperation: Operation {

0 commit comments

Comments
 (0)