dispatch_after - GCD in Swift?

前端 未结 25 2374
心在旅途
心在旅途 2020-11-21 06:07

I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it:

Can someone explain the structure of dispatch_after?

d         


        
25条回答
  •  臣服心动
    2020-11-21 06:38

    Here is synchronous version of asyncAfter in Swift:

    let deadline = DispatchTime.now() + .seconds(3)
    let semaphore = DispatchSemaphore.init(value: 0)
    DispatchQueue.global().asyncAfter(deadline: deadline) {
        dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
        semaphore.signal()
    }
    
    semaphore.wait()
    

    Along with asynchronous one:

    let deadline = DispatchTime.now() + .seconds(3)
    DispatchQueue.main.asyncAfter(deadline: deadline) {
        dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
    }
    

提交回复
热议问题