dispatch_after - GCD in Swift?

前端 未结 25 2486
心在旅途
心在旅途 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条回答
  •  旧时难觅i
    2020-11-21 06:34

    I always prefer to use extension instead of free functions.

    Swift 4

    public extension DispatchQueue {
    
      private class func delay(delay: TimeInterval, closure: @escaping () -> Void) {
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
      }
    
      class func performAction(after seconds: TimeInterval, callBack: @escaping (() -> Void) ) {
        DispatchQueue.delay(delay: seconds) {
          callBack()
        }
      }
    
    }
    

    Use as follow.

    DispatchQueue.performAction(after: 0.3) {
      // Code Here
    }
    

提交回复
热议问题