{"id":14626,"date":"2016-05-24T11:07:11","date_gmt":"2016-05-24T15:07:11","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14626"},"modified":"2016-05-24T11:07:11","modified_gmt":"2016-05-24T15:07:11","slug":"mvc-n-isolating-network-calls-from-view-controllers","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/24\/mvc-n-isolating-network-calls-from-view-controllers\/","title":{"rendered":"MVC-N: Isolating Network Calls From View Controllers"},"content":{"rendered":"<p><a href=\"https:\/\/realm.io\/news\/slug-marcus-zarra-exploring-mvcn-swift\/\">Marcus Zarra<\/a>:<\/p>\n<blockquote cite=\"https:\/\/realm.io\/news\/slug-marcus-zarra-exploring-mvcn-swift\/\"><p>We have a network controller: it talks to the internet, your local network, or even be a flat file system. It then pushes data into your cache, into your persistence engine. Here comes the exciting part: your entire view layer talks to the cache only. It does not get data from the internet directly. The ViewController&rsquo;s job is to display or update that data, not to do networking code. This design will serve you forever, and it has so many benefits that you&rsquo;ll never want to use the anti-pattern ever again.<\/p><p>[&#8230;]<\/p><p>We can use the concurrent design of the NSOperation to do our network requests. In Swift, because we cannot access the property directly for <code>finished<\/code>, we have to do hacking (which I do have a radar for, and I am hoping they will fix this). The idea behind this overrided <code>var<\/code> is that we need to tell the queue that we are done. To do that, the queue is listening for &ldquo;isFinished&rdquo;. In Objective-C, <code>isFinished<\/code> translates into the variable <code>Finished<\/code> fine; in Swift, it only listens for &ldquo;isFinished&rdquo;, it does not listen for the actual property of <code>Finish<\/code>, you cannot change it.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Marcus Zarra: We have a network controller: it talks to the internet, your local network, or even be a flat file system. It then pushes data into your cache, into your persistence engine. Here comes the exciting part: your entire view layer talks to the cache only. It does not get data from the internet [&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,800,109,27,31,30,476,71,901,268],"class_list":["post-14626","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-concurrency","tag-coredata","tag-craft","tag-ios","tag-mac","tag-networking","tag-programming","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14626","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=14626"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14626\/revisions"}],"predecessor-version":[{"id":14627,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14626\/revisions\/14627"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}