0

The function below checks whether protected data is available.

func isProtectedDataAvailable(_ completion: ((_ isProtectedDataAvailable: Bool)->Void)?) {

    DispatchQueue.main.async {
        completion?(UIApplication.shared.isProtectedDataAvailable)
    }
}

How can I change the function so that it can be called even when the main thread is paused in a setting like this:

let dispatch = DispatchGroup()

dispatch.enter()

isProtectedDataAvailable { isProtectedDataAvailable in
    //...
    dispatch.leave()
}

dispatch.wait()

The wait() above would pause the main queue and the completion handler would never be called.

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • You can't, that's how serial queues work. You can run the block in the background and use a semaphore or mutex, or maybe an NSRecursiveLock. This seems like an [xy problem](https://meta.stackexchange.com/q/66377/172958). If you give us a better idea of what exactly you need, maybe we can suggest how to use one. – Kevin Sep 23 '17 at 00:13
  • @Kevin The idea is that the function should return whether protected data is available without the developer caring about the setting it is used in. The only alternative I see is to add to the docs that one must not block the main queue when calling this function. – Manuel Sep 23 '17 at 00:20
  • Actually I think this is the same issue: https://stackoverflow.com/questions/10330679/how-to-dispatch-on-main-queue-synchronously-without-a-deadlock?rq=1 – Manuel Sep 23 '17 at 00:25
  • Yes, that would work. – Kevin Sep 23 '17 at 00:34

0 Answers0