File tree Expand file tree Collapse file tree 2 files changed +19
-10
lines changed Expand file tree Collapse file tree 2 files changed +19
-10
lines changed Original file line number Diff line number Diff line change @@ -563,29 +563,27 @@ open class OperationQueue: NSObject {
563
563
564
564
open class var current : OperationQueue ? {
565
565
#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 {
568
567
if pthread_main_np ( ) == 1 {
569
568
return OperationQueue . main
570
569
} else {
571
570
return nil
572
571
}
573
- } else {
574
- return specific!. takeUnretainedValue ( )
575
572
}
573
+
574
+ return specific. takeUnretainedValue ( )
576
575
#else
577
576
return nil
578
577
#endif
579
578
}
580
579
580
+ #if DEPLOYMENT_ENABLE_LIBDISPATCH
581
+ private static let _main = OperationQueue ( _queue: . main, maxConcurrentOperations: 1 )
582
+ #endif
583
+
581
584
open class var main : OperationQueue {
582
585
#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
589
587
#else
590
588
fatalError ( " NSOperationQueue requires libdispatch " )
591
589
#endif
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ class TestNSOperationQueue : XCTestCase {
25
25
( " test_OperationCount " , test_OperationCount) ,
26
26
( " test_AsyncOperation " , test_AsyncOperation) ,
27
27
( " test_isExecutingWorks " , test_isExecutingWorks) ,
28
+ ( " test_MainQueueGetter " , test_MainQueueGetter) ,
28
29
]
29
30
}
30
31
@@ -103,6 +104,16 @@ class TestNSOperationQueue : XCTestCase {
103
104
XCTAssertFalse ( operation. isExecuting)
104
105
XCTAssertTrue ( operation. isFinished)
105
106
}
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
+ }
106
117
}
107
118
108
119
class AsyncOperation : Operation {
You can’t perform that action at this time.
0 commit comments