Concurrency

Download swift eBook

Syntax

  • Swift 3.0

  • DispatchQueue.main // Get the main queue

  • DispatchQueue(label: "my-serial-queue", attributes: [.serial, .qosBackground]) // Create your own private serial queue

  • DispatchQueue.global(attributes: [.qosDefault]) // Access one of the global concurrent queues

  • DispatchQueue.main.async { ... } // Dispatch a task asynchronously to the main thread

  • DispatchQueue.main.sync { ... } // Dispatch a task synchronously to the main thread

  • DispatchQueue.main.asyncAfter(deadline: .now() + 3) { ... } // Dispatch a task asynchronously to the main thread to be executed after x seconds

  • Swift < 3.0

  • dispatch_get_main_queue() // Get the main queue running on the main thread

  • dispatch_get_global_queue(dispatch_queue_priority_t, 0) // Get global queue with specified priority dispatch_queue_priority_t

  • dispatch_async(dispatch_queue_t) { () -> Void in ... } // Dispatch a task asynchronously on the specified dispatch_queue_t

  • dispatch_sync(dispatch_queue_t) { () -> Void in ... } // Dispatch a task synchronously on the specified dispatch_queue_t

  • dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(nanoseconds)), dispatch_queue_t, { ... }); // Dispatch a task on the specified dispatch_queue_t after nanoseconds

Related Examples

Stats

464 Contributors: 10
Tuesday, February 14, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook