-
Notifications
You must be signed in to change notification settings - Fork 38
/
CustomScheduler.swift
45 lines (37 loc) · 1.29 KB
/
CustomScheduler.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
import Foundation
class GCDScheduler: Scheduler {
func runInForeground(task: () -> Void) {
dispatch_async(dispatch_get_main_queue(), task)
}
func runInForegroundDelayed(by delay: Double, _ task: () -> Void) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), task)
}
func runInBackground(task: () -> Void) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), task)
}
}
protocol Scheduler : class {
func runInForeground(task: () -> Void)
func runInForegroundDelayed(by delay: Double, _ task: () -> Void)
func runInBackground(task: () -> Void)
}
extension Scheduler {
func runInBackground<T>(task: () throws -> T, foregroundCompletion: (T) -> Void, foregroundFailure: (ErrorType) -> Void) {
runInBackground({ [weak self] in
guard let strongSelf = self else {
return
}
do {
let result = try task()
strongSelf.runInForeground({
foregroundCompletion(result)
})
} catch {
strongSelf.runInForeground({
foregroundFailure(error)
})
}
})
}
}