{"id":9777,"date":"2014-10-10T13:56:41","date_gmt":"2014-10-10T17:56:41","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9777"},"modified":"2014-10-10T13:56:41","modified_gmt":"2014-10-10T17:56:41","slug":"running-the-run-loop-until-a-predicate-succeeds","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/10\/10\/running-the-run-loop-until-a-predicate-succeeds\/","title":{"rendered":"Running the Run Loop Until a Predicate Succeeds"},"content":{"rendered":"<p><a href=\"http:\/\/bou.io\/CTTRunLoopRunUntil.html\">Nicolas Bouilleaud<\/a>:<\/p>\n<blockquote cite=\"http:\/\/bou.io\/CTTRunLoopRunUntil.html\"><p>By observing <code>kCFRunLoopBeforeWaiting<\/code>, we can test for completion on every loop of the RunLoop. Before sleeping (i.e. waiting for an event), the RunLoop has called everything there was to call. That&rsquo;s the right time to test for completion. This variant also solves the &ldquo;active polling&rdquo; scenarios: if the <code>polling_<\/code> flag is set, the RunLoop actually never sleeps and run continuously; <code>fulfilled_<\/code> is checked on every pass. And of course, contrary to most implementations, including my own, there&rsquo;s no &ldquo;minimal delay&rdquo;, and no additional code to handle the loop or the timeout. That should do the trick.<\/p><\/blockquote>\n<p>I&rsquo;ve seen lots of implementations of this idea. I&rsquo;m not sure what the Right Way is, but this seems like a good one. It uses <code>CFRunLoopObserver<\/code>.\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Nicolas Bouilleaud: By observing kCFRunLoopBeforeWaiting, we can test for completion on every loop of the RunLoop. Before sleeping (i.e. waiting for an event), the RunLoop has called everything there was to call. That&rsquo;s the right time to test for completion. This variant also solves the &ldquo;active polling&rdquo; scenarios: if the polling_ flag is set, 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":[69,31,30,54,71,268],"class_list":["post-9777","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-mac","tag-objective-c","tag-programming","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9777","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=9777"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9777\/revisions"}],"predecessor-version":[{"id":9778,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9777\/revisions\/9778"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}