-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dispatch.swift
48 lines (41 loc) · 1.46 KB
/
Dispatch.swift
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
44
45
46
47
48
// Created by Stanislav Feldman on 26/07/15.
// Copyright (c) 2015 Stanislav Feldman. All rights reserved.
/**
Grand Central Dispatch wrapper.
You can run code in background or main thread.
Also you can run your code after some time.
*/
class dispatch
{
class async
{
class func bg(block: dispatch_block_t) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block)
}
class func main(block: dispatch_block_t) {
dispatch_async(dispatch_get_main_queue(), block)
}
}
class sync
{
class func bg(block: dispatch_block_t) {
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block)
}
class func main(block: dispatch_block_t) {
if NSThread.isMainThread() {
block()
}
else {
dispatch_sync(dispatch_get_main_queue(), block)
}
}
}
class after {
class func bg(when: Double, block: dispatch_block_t) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(when*Double(NSEC_PER_SEC))), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)!, block)
}
class func main(when: Double, block: dispatch_block_t) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(when*Double(NSEC_PER_SEC))), dispatch_get_main_queue(), block)
}
}
}