nghialv blog

practice makes perfect

GCD - a Wrapper of Grand Central Dispatch

| Comments

GCD is a wrapper of Grand Central Dispatch written in Swift.

source code : https://github.com/nghialv/GCD

Examples

- gcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// submit your code for asynchronous execution on a global queue with high priority
gcd.async(.High) {
    // your code
}

// or with main thread
gcd.async(.Main) {
    // your code
}

gcd.async(.Default) {
    // your code
    gcd.async(.Main) {
        // code run on main thread
    }
}

// with your custom queue
let myQueue = GCDQueue(serial: "myQueue")
gcd.async(.Custom(myQueue)) {
    // your code
}

// run with delay
gcd.async(.Background, delay: 5.0) {
    // your code
}

// sync code
gcd.sync(.Main) {
    // your code
}

// apply
gcd.apply(.Default, 10) { index in
    // your code
}

// once
var onceToken: GCDOnce = 0
gcd.once(&onceToken) {
    // your code
}
- manage group of block with GCDGroup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// create group
let group = GCDGroup()

// you can add async code to group
group.async(.Defaul) {
    // your code
}

// you can set notify for this group
group.notify(.Main) {
    // your code
}

// or wait synchronously for block in group to complete and timeout is 10 seconds
group.wait(10)
- create your custom queue with CGDQueue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// create a serial queue
let serialQueue = GCDQueue(serial: "mySerialQueue")

// create a concurrent queue
let concurrentQueue = GCDQueue(concurrent: "myConcurrentQueue")

// you can submit async barrier to queue
myQueue.asyncBarrier {
    // your code
}

// or sync code
myQueue.syncBarrier {
    // your code
}

Integration

Just drag GCD.swift file to the project tree

Comments