Get current dispatch queue?

后端 未结 11 1280
难免孤独
难免孤独 2020-12-23 16:28

I have a method which should support being called from any queue, and should expect to.

It runs some code in a background thread itself, and then uses dispatch

相关标签:
11条回答
  • 2020-12-23 16:58

    If you are working with an NSOperationQueue, it can provide the current dispatch queue for you.

    NSOperationQueue has the class function [NSOperationQueue currentQueue], which returns the current queue as a NSOperationQueue object. To get the dispatch queue object you can use [NSOperationQueue currentQueue].underlyingQueue, which returns your currrent queue as a dispatch_queue_t.

    Swift 3:

    if let currentDispatch = OperationQueue.current?.underlyingQueue {
        print(currentDispatch)
    }
    

    - works for main queue!

    0 讨论(0)
  • 2020-12-23 16:59

    Based on the source from SQLite.swift.
    If you want to check whether you're on own special dispatch queue:

    class Worker {
        private static let queueKey = DispatchSpecificKey<Int>()
        private lazy var queueContext = unsafeBitCast(self, to: Int.self)
        private lazy var queue: DispatchQueue = {
            let value = DispatchQueue(label: "com.example.App.Worker")
            value.setSpecific(key: Worker.queueKey, value: queueContext)
            return value
        }()
    
        func test(x: Int) -> Int {
            return dispatchSync {
                return x > 2 ? test(x: x - 1) * x : x
            }
        }
    
        private func dispatchSync<T>(_ block: () throws -> T) rethrows -> T {
            if DispatchQueue.getSpecific(key: Worker.queueKey) != queueContext {
                return try queue.sync(execute: block)
            }
            return try block()
        }
    }
    
    let worker = Worker()
    worker.test(x: 5)
    
    0 讨论(0)
  • 2020-12-23 17:01

    You do have the option of "dispatch_get_current_queue()", however the iOS 6.1 SDK defines this API with these disclaimers:

    "Recommended for debugging and logging purposes only:"

    and

    "This function is deprecated and will be removed in a future release.".

    Here's another related question with some alternatives you can consider if you want code that's future-proof.

    0 讨论(0)
  • 2020-12-23 17:04

    As an alternative approach to this NSOBject's method performSelector:withObject:afterDelay: dispatches the call on the current thread's run loop. According to the docs:

    This method sets up a timer to perform the aSelector message on the current thread’s run loop.

    Obviously I'm suggesting using this with a delay of zero, which, according to the docs again:

    Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

    Unfortunately it requires exactly one argument, so some workarounds might be needed if your method takes more or less.

    One other thing I noted is that this method is not available for protocols, but implementations alone. This is due to this method living in an NSObject category, and not in the NSObject interface (see PS below). This can easily be fixed by casting to id.

    PS: Two different NSObjects exist, a protocol and an implementation. Notice NSObject declaration:

    @interface NSObject <NSObject> { ... }
    

    It might seem odd, but one is being declared (after @interface) and the other one is a previously declared protocol (between < and >). When declaring a protocol that extends NSObject (ie., @protocol Foo <NSObject>) the protocol inherits the methods from the later, but not the former. Eventually the protocol is implemented by some class that inherits from the NSObject implementation, so all instances inheriting from the NSObject implementation still holds. But I'm getting off topic.

    0 讨论(0)
  • 2020-12-23 17:04

    To get the label of the current queue and compare to a defined label using.

    let queueName = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)
    
    0 讨论(0)
提交回复
热议问题