Created
January 9, 2016 23:23
-
-
Save garkhipov/bf1c1e586a2f15c99a46 to your computer and use it in GitHub Desktop.
Quick and dumb main thread watchdog that fires *before* the main thread is done
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
// WARNING: No thought involved while writing this, introduce some prior to production use. I warned you. | |
var timer: dispatch_source_t! | |
func startWatchdog() { | |
let q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) | |
guard let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q) else { return } | |
self.timer = timer | |
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC / 10, (UInt64(1) * NSEC_PER_SEC) / 10) | |
dispatch_source_set_event_handler(timer) { | |
var flag = false | |
dispatch_async(dispatch_get_main_queue()) { | |
flag = true | |
} | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC / 10)), q) { | |
if !flag { | |
// Break here and look at your main thread call stack | |
print("lag") | |
} | |
} | |
} | |
dispatch_resume(timer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment