{"id":12636,"date":"2015-10-22T10:14:28","date_gmt":"2015-10-22T14:14:28","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12636"},"modified":"2015-10-23T10:31:46","modified_gmt":"2015-10-23T14:31:46","slug":"gcd-sugar-for-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/10\/22\/gcd-sugar-for-swift\/","title":{"rendered":"GCD Sugar for Swift"},"content":{"rendered":"<p>Tobias Due Munk&rsquo;s <a href=\"https:\/\/github.com\/duemunk\/Async\">Async<\/a> (via <a href=\"http:\/\/swiftsandbox.io\/issues\/8\">Swift Sandbox<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/duemunk\/Async\">\n<p>Async sugar looks like this:<\/p>\n<pre>Async.background {\n    println(\"This is run on the background queue\")\n}.main {\n    println(\"This is run on the main queue, after the previous block\")\n}<\/pre>\n<p>Instead of the familiar syntax for GCD:<\/p>\n<pre>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {\n    println(\"This is run on the background queue\")\n\n    dispatch_async(dispatch_get_main_queue(), {\n        println(\"This is run on the main queue, after the previous block\")\n    })\n})<\/pre>\n<p>The way it work is by using the new notification API for GCD introduced in OS X 10.10 and iOS 8. Each chaining block is called when the previous queue has finished.<\/p>\n<\/blockquote>\n<p>Update (2015-10-23): See also <a href=\"https:\/\/github.com\/khoiln\/Run\">Run<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tobias Due Munk&rsquo;s Async (via Swift Sandbox): Async sugar looks like this: Async.background { println(\"This is run on the background queue\") }.main { println(\"This is run on the main queue, after the previous block\") } Instead of the familiar syntax for GCD: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { println(\"This is run on the background queue\") dispatch_async(dispatch_get_main_queue(), { println(\"This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[800,880,31,904,30,903,74,71,901],"class_list":["post-12636","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-grand-central-dispatch-gcd","tag-ios","tag-ios-8","tag-mac","tag-mac-os-x-10-10-yosemite","tag-opensource","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=12636"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12636\/revisions"}],"predecessor-version":[{"id":12647,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12636\/revisions\/12647"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}