{"id":18000,"date":"2017-05-16T16:45:51","date_gmt":"2017-05-16T20:45:51","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18000"},"modified":"2017-06-04T19:35:47","modified_gmt":"2017-06-04T23:35:47","slug":"all-about-concurrency-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/05\/16\/all-about-concurrency-in-swift\/","title":{"rendered":"All About Concurrency in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.uraimo.com\/2017\/05\/07\/all-about-concurrency-in-swift-1-the-present\/\">Umberto Raimondi<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.uraimo.com\/2017\/05\/07\/all-about-concurrency-in-swift-1-the-present\/\"><p>Swift 3 introduces a new function to perform assertions on the current execution context, that allows to verify if a closure is being executed on the expected queue. We can build predicates using the three enum cases of <code>DispatchPredicate<\/code>: <code>.onQueue<\/code>, to verify that we are on a specific queue, <code>.notOnQueue<\/code>, to verify the opposite and <code>.onQueueAsBarrier<\/code> to check if the current closure or work item are acting as a barrier on a queue.<\/p><\/blockquote>\n<p>Update (2017-06-04): <a href=\"https:\/\/www.swiftbysundell.com\/posts\/a-deep-dive-into-grand-central-dispatch-in-swift\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/posts\/a-deep-dive-into-grand-central-dispatch-in-swift\">\n<p>One common misconception about GCD is that &ldquo;once you schedule a task it cannot be cancelled, you need to use the <code>Operation<\/code> API for that&rdquo;. While this used to be true, with iOS 8 &amp; macOS 10.10 <code>DispatchWorkItem<\/code> was introduced, which provide this exact functionality in a very easy to use API.<\/p>\n<p>[&#8230;]<\/p>\n<p>As you can see above, using <code>DispatchWorkItem<\/code> is actually a lot simpler and nicer in Swift than having to use a <code>Timer<\/code> or <code>Operation<\/code>, thanks to trailing closure syntax and how well GCD imports into Swift. You don&rsquo;t need <code>@objc<\/code> marked methods, or <code>#selector<\/code>, it can all be done with closures.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Umberto Raimondi: Swift 3 introduces a new function to perform assertions on the current execution context, that allows to verify if a closure is being executed on the expected queue. We can build predicates using the three enum cases of DispatchPredicate: .onQueue, to verify that we are on a specific queue, .notOnQueue, to verify the [&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,1380,30,1381,71,901],"class_list":["post-18000","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-grand-central-dispatch-gcd","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18000","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=18000"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18000\/revisions"}],"predecessor-version":[{"id":18105,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18000\/revisions\/18105"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}