Skip to content

Instantly share code, notes, and snippets.

@jacobvanorder
Created March 31, 2015 18:49
Show Gist options
  • Save jacobvanorder/ac8dee5f42de18f4f359 to your computer and use it in GitHub Desktop.
Save jacobvanorder/ac8dee5f42de18f4f359 to your computer and use it in GitHub Desktop.
Synchronous vs. Asynchronous WatchKit call
func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
/* Synchronous
let response : (DateTime?, NSError?) = Network.fetchDateTime()
if let checkedDateTime = response.0 {
reply(["response": NSKeyedArchiver.archivedDataWithRootObject(checkedDateTime)])
}
else if let checkedError = response.1 {
reply(["error": NSKeyedArchiver.archivedDataWithRootObject(checkedError)])
}
else {
reply(["error": NSKeyedArchiver.archivedDataWithRootObject("whoops")])
NSLog("Error")
}
*/
/*Asynchronous*/
let sem = dispatch_semaphore_create(0)
Network.asynchFetchDateTime {
(dateTime, error) -> Void in
if let checkedDateTime = dateTime {
reply(["response": NSKeyedArchiver.archivedDataWithRootObject(checkedDateTime)])
}
else if let checkedError = error {
reply(["error": NSKeyedArchiver.archivedDataWithRootObject(checkedError)])
}
dispatch_semaphore_signal(sem)
}
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment