Skip to content

Commit d8c2af3

Browse files
weissiphausler
authored andcommitted
fix Operation.isExecuting (#1080)
1 parent 5b07ca5 commit d8c2af3

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Foundation/Operation.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ open class Operation : NSObject {
4848
}
4949

5050
open func start() {
51+
lock.lock()
52+
_executing = true
53+
lock.unlock()
5154
main()
55+
lock.lock()
56+
_executing = false
57+
lock.unlock()
5258
finish()
5359
}
5460

@@ -85,7 +91,12 @@ open class Operation : NSObject {
8591
}
8692

8793
open var isExecuting: Bool {
88-
return _executing
94+
let wasExecuting: Bool
95+
lock.lock()
96+
wasExecuting = _executing
97+
lock.unlock()
98+
99+
return wasExecuting
89100
}
90101

91102
open var isFinished: Bool {

TestFoundation/TestNSOperationQueue.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class TestNSOperationQueue : XCTestCase {
2323
return [
2424
("test_OperationPriorities", test_OperationPriorities),
2525
("test_OperationCount", test_OperationCount),
26-
("test_AsyncOperation", test_AsyncOperation)
26+
("test_AsyncOperation", test_AsyncOperation),
27+
("test_isExecutingWorks", test_isExecutingWorks),
2728
]
2829
}
2930

@@ -68,6 +69,26 @@ class TestNSOperationQueue : XCTestCase {
6869
XCTAssertEqual(msgOperations[3], "Operation4 executed")
6970
}
7071

72+
func test_isExecutingWorks() {
73+
class _OperationBox {
74+
var operation: Operation?
75+
init() {
76+
self.operation = nil
77+
}
78+
}
79+
let queue = OperationQueue()
80+
let opBox = _OperationBox()
81+
let op = BlockOperation(block: { XCTAssertEqual(true, opBox.operation?.isExecuting) })
82+
opBox.operation = op
83+
XCTAssertFalse(op.isExecuting)
84+
85+
queue.addOperation(op)
86+
queue.waitUntilAllOperationsAreFinished()
87+
XCTAssertFalse(op.isExecuting)
88+
89+
opBox.operation = nil /* break the reference cycle op -> <closure> -> opBox -> op */
90+
}
91+
7192
func test_AsyncOperation() {
7293
let operation = AsyncOperation()
7394
XCTAssertFalse(operation.isExecuting)

0 commit comments

Comments
 (0)