1. ホーム
  2. Swift

swift 4.0でのdispatch_async,dispatch_afterの使用について

2022-03-02 10:56:45

Swift 2.x

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    let image = self.loadImage()
    // go back to the main thread to update the UI
    dispatch_async(dispatch_get_main_queue()) {
        self.imageView.image = image
    }
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
    print("test")
}

DispatchQueue.global(qos: .userInitiated).async {
    let image = self.loadImage()
    // back to the main thread 
    DispatchQueue.main.async {
        self.imageView.image = image
    }
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) { 
    print("Are we there yet? ")
}










Swift 4.0


DispatchQueue.global(qos: .userInitiated).async {
    let image = self.loadImage()
    // back to the main thread 
    DispatchQueue.main.async {
        self.imageView.image = image
    }
}

DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) { 
    print("Are we there yet? ")
}