Skip to content

Commit 575a9a0

Browse files
authored
Merge pull request #1094 from bubski/operation-queue
2 parents e3181ee + c600cfa commit 575a9a0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Foundation/Operation.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -563,29 +563,27 @@ open class OperationQueue: NSObject {
563563

564564
open class var current: OperationQueue? {
565565
#if DEPLOYMENT_ENABLE_LIBDISPATCH
566-
let specific = DispatchQueue.getSpecific(key: OperationQueue.OperationQueueKey)
567-
if specific == nil {
566+
guard let specific = DispatchQueue.getSpecific(key: OperationQueue.OperationQueueKey) else {
568567
if pthread_main_np() == 1 {
569568
return OperationQueue.main
570569
} else {
571570
return nil
572571
}
573-
} else {
574-
return specific!.takeUnretainedValue()
575572
}
573+
574+
return specific.takeUnretainedValue()
576575
#else
577576
return nil
578577
#endif
579578
}
580579

580+
#if DEPLOYMENT_ENABLE_LIBDISPATCH
581+
private static let _main = OperationQueue(_queue: .main, maxConcurrentOperations: 1)
582+
#endif
583+
581584
open class var main: OperationQueue {
582585
#if DEPLOYMENT_ENABLE_LIBDISPATCH
583-
let specific = DispatchQueue.main.getSpecific(key: OperationQueue.OperationQueueKey)
584-
if specific == nil {
585-
return OperationQueue(_queue: DispatchQueue.main, maxConcurrentOperations: 1)
586-
} else {
587-
return specific!.takeUnretainedValue()
588-
}
586+
return _main
589587
#else
590588
fatalError("NSOperationQueue requires libdispatch")
591589
#endif

TestFoundation/TestNSOperationQueue.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestNSOperationQueue : XCTestCase {
2525
("test_OperationCount", test_OperationCount),
2626
("test_AsyncOperation", test_AsyncOperation),
2727
("test_isExecutingWorks", test_isExecutingWorks),
28+
("test_MainQueueGetter", test_MainQueueGetter),
2829
]
2930
}
3031

@@ -103,6 +104,16 @@ class TestNSOperationQueue : XCTestCase {
103104
XCTAssertFalse(operation.isExecuting)
104105
XCTAssertTrue(operation.isFinished)
105106
}
107+
108+
func test_MainQueueGetter() {
109+
XCTAssertTrue(OperationQueue.main === OperationQueue.main)
110+
111+
/*
112+
This call is only to check if OperationQueue.main returns a living instance.
113+
There used to be a bug where subsequent OperationQueue.main call would return a "dangling pointer".
114+
*/
115+
XCTAssertFalse(OperationQueue.main.isSuspended)
116+
}
106117
}
107118

108119
class AsyncOperation: Operation {

0 commit comments

Comments
 (0)