Skip to content

Instantly share code, notes, and snippets.

@alexeckermann
Created November 10, 2014 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeckermann/ca984134d64910603ad3 to your computer and use it in GitHub Desktop.
Save alexeckermann/ca984134d64910603ad3 to your computer and use it in GitHub Desktop.
GCD dispatch_resume test for Swift
import Foundation
import XCPlayground
import Dispatch
class Looper {
var loopQueue : dispatch_queue_t!
var tickSource : dispatch_source_t!
init() {
loopQueue = dispatch_queue_create("com.alexeckermann.looper", nil)
println("looper initd")
}
func loop() {
println("loop called")
tickSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, loopQueue)
dispatch_source_set_timer(tickSource, dispatch_time(DISPATCH_TIME_NOW, 0), (200 * NSEC_PER_MSEC), (50 * NSEC_PER_MSEC))
dispatch_source_set_event_handler(tickSource) { println("success!") } // Looking for this to print to the output
dispatch_async(loopQueue, { println("queue works") })
dispatch_resume(tickSource) // Comment this out if it raises an exception/
}
}
var looper = Looper()
looper.loop()
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment