Last active
December 1, 2019 23:27
-
-
Save Inferis/0813bf742742774d55fa to your computer and use it in GitHub Desktop.
Beginnings of a GCD wrapper in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
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) | |
} | |
} | |
} | |
// after by @stanislavfeldman | |
class after { | |
class func bg(when: dispatch_time_t, block: dispatch_block_t) { | |
dispatch_after(when, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)!, block) | |
} | |
class func main(when: dispatch_time_t, block: dispatch_block_t) { | |
dispatch_after(when, dispatch_get_main_queue(), block) | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import QuartzCore | |
class ViewController: UIViewController { | |
@IBOutlet weak var label: UILabel | |
@IBOutlet weak var counter: UILabel | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
dispatch.async.bg { | |
var count = 0; | |
for index in 0..10000000 { | |
dispatch.sync.main { | |
self.counter.text = "\(index)" | |
} | |
} | |
} | |
} | |
@IBAction func clickety() { | |
label.text = NSDate.date().description | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've added support for dispatch groups and blogged about my implementation.
http://adamwrobel.com/blog/2016/05/08/swift-gcd-wrapper/