/DispatchKit

An idiomatic Swift wrapper for the Grand Central Dispatch (GCD) Framework

Primary LanguageSwiftMIT LicenseMIT

DispatchKit – iOS and OS X Framework written in Swift

The project aims to provide an idiomatic Swift language wrapper for the Grand Central Dispatch Framework, also known as GCD, or libdispatch library.

If you are familiar with the C-based GCD API, you can continually apply your knowledge when writing Swift code, because the DispatchKit API closely matches the original API.

Otherwise, if you have little or no experience with GCD, then probably the DispatchKit is a good place to start playing with, as it allows you to learn GCD in a much more cleaner way. Swift is a more type safe and less error-prone language than Objective-C, and the DispatchKit uses strict types and short method names to wrap GCD types and functions.

In addition, the DispatchKit is assumed to have zero overhead compared to GCD code written in C or Objective-C using the original API. That's because DispatchKit wrappers are just tiny structs, which do not require expensive memory allocation themselves.

Usage examples are provided in Swift using DispatchKit, compared to Objective-C code using the original GCD API.

Swift Objective-C
let mainQueue = Dispatch.mainQueue


let globalQueue = Dispatch.globalQueue


let backgroundQueue = Dispatch.getGlobalQueue(priority: .Background)


let serialQueue = DispatchQueue("com.example.serial-queue")
dispatch_queue_t mainQueue =
    dispatch_get_main_queue();

dispatch_queue_t globalQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_queue_t backgroundQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_queue_t serialQueue =
    dispatch_queue_create("com.example.serial-queue",
                          DISPATCH_QUEUE_SERIAL);

As shown an the previous example, the GCD constants are mapped into the Swift enumerations.

For instance, the DISPATCH_QUEUE_PRIORITY_* constants are mapped as follows:

enum DispatchQueuePriority : dispatch_queue_priority_t {
    case High = DISPATCH_QUEUE_PRIORITY_HIGH
    case Default = DISPATCH_QUEUE_PRIORITY_DEFAULT
    case Low = DISPATCH_QUEUE_PRIORITY_LOW
    case Background = DISPATCH_QUEUE_PRIORITY_BACKGROUND
}

Actual mapping is performed by the Swift compiler behind the scenes. Refer to dk_enums.h for the corresponding Objective-C code.

Swift Objective-C
serialQueue.sync {
    // sync task
}

let concurrentQueue = DispatchQueue("com.example.concurrent-queue",
                                    attr: .Concurrent)


concurrentQueue.apply(42) { i in
    println("item #\(i)")
}

Dispatch.globalQueue.async {
    // async task
}

Dispatch.mainQueue.after(.Now + .Seconds(42)) {
    // ... code to be executed after a delay of 42 seconds
}
dispatch_sync(serialQueue, ^{
    // sync task
});

dispatch_queue_t concurrentQueue =
    dispatch_queue_create("com.example.concurrent-queue",
                          DISPATCH_QUEUE_CONCURRENT);

dispatch_apply(42, concurrentQueue , ^(size_t i){
    NSLog(@"item #%ld", (long)i);
});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // async task
});

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 42 * NSEC_PER_SEC),
               dispatch_get_main_queue(), ^{
    // ... code to be executed after a delay of 42 seconds
});

The previous example uses time expressions. Other forms of time expressions are also possible:

.Now + .Seconds(3) + .Milliseconds(145) + .Microseconds(926) + .Nanoseconds(535)
.WallClock(timespec) + .Days(5) + .Hours(40)

Refer to DispatchTime.swift for further details.

An additional .Forever constant is used by default with wait() method defined for groups and semaphores.

Swift Objective-C
let group = DispatchGroup()

globalQueue.async(group) {
    // task 1
}

globalQueue.async(group) {
    // task 2
}

group.notify(globalQueue) {
    // queued after tasks 1 and 2 were finished
}

group.wait()
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, globalQueue,^{
    // task 1
});

dispatch_group_async(group, globalQueue,^{
    // task 2
});

dispatch_group_notify(group, globalQueue,^{
    // queued after tasks 1 and 2 were finished
});

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
Swift Objective-C
let sema4 = DispatchSemaphore(4);

concurrentQueue.async {
    sema4.wait()
    // access some finite resource
    sema4.signal()
}
dispatch_semaphore_t sema4 = dispatch_semaphore_create(4);

dispatch_async(concurrentQueue, ^{
    dispatch_semaphore_wait(sema4, DISPATCH_TIME_FOREVER);
    // access some finite resource
    dispatch_semaphore_signal(sema4);
});

For details, refer to DispatchIO.swift and DispatchData.swift.

For details, refer to DispatchSource.swift and various flags declared in DispatchSourceType.swift and dk_enums.h.

The DispatchKit is designed to be source-compatible with iOS 7 SDK, binary-compatible with iOS 7 platform.

Follow @DispatchKit on Twitter. Feel free to ask questions and provide your suggestions.

The DispatchKit is available under the MIT License.