<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michael Liberatore on Medium]]></title>
        <description><![CDATA[Stories by Michael Liberatore on Medium]]></description>
        <link>https://medium.com/@mliberatore?source=rss-ec13c225c26f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Dq3R5cRaxa4Ym-FJrpcs8w.jpeg</url>
            <title>Stories by Michael Liberatore on Medium</title>
            <link>https://medium.com/@mliberatore?source=rss-ec13c225c26f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 19:36:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mliberatore/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Insidious Bugs #1: Today Extensions]]></title>
            <link>https://medium.com/inside-lickability/insidious-bugs-1-today-extensions-345fb3bcd6a2?source=rss-ec13c225c26f------2</link>
            <guid isPermaLink="false">https://medium.com/p/345fb3bcd6a2</guid>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[insidious-bugs]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Michael Liberatore]]></dc:creator>
            <pubDate>Tue, 17 Apr 2018 17:58:24 GMT</pubDate>
            <atom:updated>2018-08-10T13:37:40.096Z</atom:updated>
            <content:encoded><![CDATA[<h4>This app could not be installed at this time.</h4><p>Today Lickability is relaunching <a href="http://insidiousbugs.com/">Insidious Bugs</a> on our blog. Insidious Bugs is an occasional series in which we share stories of overcoming obscure issues in iOS development with the goal of saving you the time we lost, should you encounter the issues yourself.</p><h4>The Bug</h4><p>In this first entry, we conquer an instance of Xcode’s friendly “This app could not be installed at this time” message. We encountered this when attempting to run our project that includes a <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/Today.html">Today Extension</a>, or <em>widget, </em>in the iOS Simulator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aM4CgC_PBvYw81V9RMBQug.png" /><figcaption>Uhhhhhhh… okay?</figcaption></figure><h4>The Solution</h4><p>Would you believe that all it took to solve this issue was this seemingly useless file? Well, <em>that</em> and many, many hours of failed attempts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/891feb87e241d44a5e1bce4fbd6b3171/href">https://medium.com/media/891feb87e241d44a5e1bce4fbd6b3171/href</a></iframe><h4>How We Got Here</h4><p>“This app could not be installed at this time” doesn’t give us a lot to work with, but let’s explore the somewhat unique setup that got us here.</p><p>We recently added Today Extensions to multiple projects, which all share the same layout. We tend to keep things <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> (don’t repeat yourself), so we made our approach generic and reusable across multiple projects via a shared framework. In our framework, the Today Extension displays an arbitrary number of cells that have a thumbnail image and two labels. The framework doesn’t care what type of content is to be displayed, whether it’s social media posts, news, reminders, etc.—it simply formats and displays the data written to the shared container by the host app in the exact same way. Here’s an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V2V7RGqgcFIIZHAxJeaF2A.png" /><figcaption>You’d buy this app, right?</figcaption></figure><p>Hooray! This will save us time and tedious boilerplate as we add our Today Extensions to our projects. We simply need to add the framework and associate the view controller in MainInterface.storyboard (part of the Today Extension’s target) with the one found in the framework. At least that’s what we thought. Everything compiled just fine, but we couldn’t debug our app. <em>“This app could not be installed at this time.”</em> 🤔 Since Xcode’s message didn’t point us in any particular direction on how to resolve the bug, we searched log files for anything that was amiss. Every time we’d attempt to build and run, a message similar to the following was printed to mobile_installation.log.0 within the simulator’s logs folder:</p><pre>&lt;err&gt; (0x700008983000) -[MIExecutableBundle makeExecutableWithError:]: 1162: Failed to chmod /Users/Michael/Library/Developer/CoreSimulator/Devices/51417E58-232D-45D5-9DAB-EB4C6F0AA301/data/Library/Caches/com.apple.containermanagerd/Bundle/Application/0D7DF887-D2D6-45F7-86DB-AACE8F2AE875/LickabilityTweets.app/PlugIns/TodayExtension.appex/TodayExtension : No such file or directory (NSPOSIXErrorDomain:2 (null))</pre><p>This error was more descriptive, but still didn’t lead us to a solution. “Why wouldn’t the app extension exist on disk after a build succeeded?” we asked, before making dozens of trial and error attempts across several hours to no avail.</p><p>Eventually, we found that moving the view controller back to the Today Extension target would resolve the issue, but we weren’t satisfied with duplicating that file across multiple projects. In a stroke of genius (read: luck) we asked each other, “Wait… is it the fact that the view controller needs to be a part of the extension target, or is it just that we linked <em>something</em> to the target that made the difference?” After all, our “Compile Sources” build phase required <em>nothing,</em> since by design, any executable code used by the widget would live in our shared framework.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*XSrAHta_Qzyc80X5brJ-lg.png" /><figcaption>What a lonely target 😢</figcaption></figure><p>So we just added <em>something</em> to our target to see if it made any difference. A single Swift file without any code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bac59ed9a27fdf82e7101367987328db/href">https://medium.com/media/bac59ed9a27fdf82e7101367987328db/href</a></iframe><p><em>Build Succeeded! Installing! Success! It worked… that was it?!</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OEul8X3XJ2TFnr-995k9pQ.png" /><figcaption>😠😡😭</figcaption></figure><p>We thought we had it. But at least we could build <em>and run</em> now. On launch of the Today Extension target, Xcode’s console would display the following:</p><pre>Unknown class _TtC19SharedExtensionCode19TodayViewController in Interface Builder file.</pre><p>It seemed as though despite the fact that we were selecting the appropriate view controller class from the appropriate framework within MainInterface.storyboard, our class was still nowhere to be found at run time. After much more trial and error, we added a bit more code to FixEmptyTarget.swift. We wanted to make sure everything was properly linked. We imported our shared framework into the file to see if it built (it did) and wrote some test code to see if we could actually make reference to the view controller class (we could). 🧐 “How come we can reference this view controller from a different framework in code, but not in a storyboard?” we pondered. We tried running this, just to see if anything more helpful would print to the console with our reference to the disappearing view controller in code, and…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qc887uazPV4PLyVJScKFg.png" /><figcaption>Whaaaaaaaaaaaaaaaaaaaaat 🤯</figcaption></figure><blockquote><strong>Michael: </strong>That… did it?</blockquote><blockquote><strong>Andrew: </strong>It’s time to relaunch Insidious Bugs.</blockquote><p>Simply referencing our view controller in code and adding an otherwise useless file to our target solved this mysterious series of problems, and now we finally have our Today Extension framework. The full, documented version of FixEmptyTarget.swift looks like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e5122ce9b26af2566a342d18b90ca7b/href">https://medium.com/media/4e5122ce9b26af2566a342d18b90ca7b/href</a></iframe><p>Bananas, right? We sincerely hope you don’t encounter an issue like this in your travels. And if you do, we hope this post helped you get back up and running quickly!</p><h4>Radars</h4><p>Here are the bugs we’ve filed for Apple about these issues:</p><ul><li><a href="http://www.openradar.me/radar?id=4983936539164672">Today Extension with no linked files fails to install in the simulator (39237943, closed as intended behavior)</a></li><li><a href="http://www.openradar.me/radar?id=5035739347681280">Follow-up: Difficult to understand error message with today extension that has no linked files (39974757)</a></li><li>View controller referenced only from a storyboard not found at runtime <a href="http://www.openradar.me/radar?id=4952377790562304">(39238390, Closed as duplicate of 4691676)</a></li></ul><p><a href="https://blog.lickability.com/@mliberatore"><em>Michael Liberatore</em></a><em> is an iOS Engineer at </em><a href="https://lickability.com/"><em>Lickability</em></a><em> in New York. When he’s not writing code, you can find him drumming, running in Prospect Park, or enjoying a </em><a href="http://barnotes.co/recipes/whiskey-business--2"><em>Whiskey Business</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=345fb3bcd6a2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/inside-lickability/insidious-bugs-1-today-extensions-345fb3bcd6a2">🐞 Insidious Bugs #1: Today Extensions</a> was originally published in <a href="https://medium.com/inside-lickability">Lickability Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How We Smoke Test Pull Requests with Git Revert]]></title>
            <link>https://medium.com/inside-lickability/how-we-smoke-test-pull-requests-with-git-revert-c7eb11904404?source=rss-ec13c225c26f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7eb11904404</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[tips]]></category>
            <dc:creator><![CDATA[Michael Liberatore]]></dc:creator>
            <pubDate>Tue, 27 Feb 2018 17:21:32 GMT</pubDate>
            <atom:updated>2018-03-02T14:22:58.936Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eqDwpAbSU09AN6Oc." /><figcaption>Photo by <a href="https://unsplash.com/@yoshginsu?utm_source=medium&amp;utm_medium=referral">Yosh Ginsu</a></figcaption></figure><p><em>You’ve been there</em>: you get assigned to review a pull request for a work-in-progress feature you know little about, and you have trouble seeing how it all fits together with the work that’s yet to come. The code looks good, but you’re hesitant to add your stamp of approval and hit the merge button. Even with thorough UI and unit tests, you want to test the new feature and see it in action yourself, just as the author did during development. Rather than making the reviewer follow a complex set of instructions to add code or print statements to verify that the in-progress feature is working, we’ve landed on this <em>one weird trick</em>. Internally, we call it the “git revert trick.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hfOYd2a-HrcVdMj_HsWpxQ.png" /><figcaption>An example of the git revert trick in action!</figcaption></figure><h3>How It Works</h3><p>When a developer is unable to provide clear “How to Test” steps without requiring additional complicated changes, they can now commit and revert the test code that they used to initially <a href="https://en.wikipedia.org/wiki/Smoke_testing_(software)">smoke test</a> their feature and provide the git command necessary to re-stage the test code in the reviewer’s working copy. The command is git revert -n &lt;revert-test-code-sha&gt;. This reverts the <em>removal</em> of the test code, with <a href="https://git-scm.com/docs/git-revert#git-revert--n">-n</a> (short for <a href="https://git-scm.com/docs/git-revert#git-revert---no-commit">--no-commit</a>) preventing the revert from being committed, allowing the user to see the working copy changes and easily discard them when they’ve finished testing.</p><p>We now frequently make use of this command in our pull request reviews. It removes room for error in the reviewer’s setup process, provides in-context and diff-able code for exercising the changes, and prevents any throwaway test code for in-progress features from ever getting merged.</p><p><a href="https://github.com/Lickability/PinpointKit/pull/177"><strong>See it in action</strong></a><strong> in our open source library PinpointKit and let us know what you think of this technique here or on Twitter at </strong><a href="https://twitter.com/lickability"><strong>@lickability</strong></a><strong>.</strong></p><p><a href="https://blog.lickability.com/@mliberatore"><em>Michael Liberatore</em></a><em> is an iOS Engineer at </em><a href="https://lickability.com/"><em>Lickability</em></a><em> in New York. When he’s not writing code, you can find him drumming, running in Prospect Park, or enjoying a </em><a href="http://barnotes.co/recipes/whiskey-business--2"><em>Whiskey Business</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7eb11904404" width="1" height="1" alt=""><hr><p><a href="https://medium.com/inside-lickability/how-we-smoke-test-pull-requests-with-git-revert-c7eb11904404">How We Smoke Test Pull Requests with Git Revert</a> was originally published in <a href="https://medium.com/inside-lickability">Lickability Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Our View on View Models]]></title>
            <link>https://medium.com/inside-lickability/our-view-on-view-models-4bb1d0675038?source=rss-ec13c225c26f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bb1d0675038</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Michael Liberatore]]></dc:creator>
            <pubDate>Mon, 30 Jan 2017 18:26:50 GMT</pubDate>
            <atom:updated>2017-01-30T18:34:32.504Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the past few years, the <a href="https://www.objc.io/issues/13-architecture/mvvm/">Model-View-ViewModel</a> (MVVM) design pattern has become popular in iOS application architecture, battling the <a href="http://khanlou.com/2015/12/massive-view-controller/">massive view controller</a> problem that traditional <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html">Model-View-Controller</a> (MVC) architecture left us with. Aided by a fourth component, the view model, MVVM offers a means of breaking some presentation logic out of controllers.</p><p>View models and their many definitions can leave developers new to the topic scratching their heads. It’s also possible to shift too much responsibility to view models in an effort to make view controllers lighter. We don’t strictly follow MVVM at Lickability, but we do make extensive use of view models, and we think it’s worth sharing exactly how. We’ll explore our approach to using view models, what they are and aren’t in our codebases, and why we find them delightful to work with in iOS and Swift.</p><h4>View Model: A Simple* Definition</h4><p>We broadly define “view model” as <em>a type that encapsulates the data needed to populate a particular kind of view</em>. More succinctly, a view model is <em>a view’s model</em>. These basic definitions leave something to be desired. Do our view models merely store data and remain void of any operations? No — we allow them to perform operations strictly in the aid of displaying data, so let’s expand this definition to <em>a type that encapsulates the data needed to populate a particular kind of view and the presentation logic needed to transform the data into properties that can be rendered</em>. Okay, but what qualifies as “presentation logic,” and what does “transform” mean in this context? 😵</p><h4>View Model: A Concrete Example</h4><p>Let’s start over, and discuss view models in terms of UIKit and iOS application architecture with an example. Ultimately, we’re going to build a cell that can display a comment about a blog post for an interface that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*BmNhSdwPRgJZBp4WBuczWA.png" /><figcaption>The completed interface with comment cells backed by view models.</figcaption></figure><p>We’ll start with a view. Specifically a UITableViewCell, which is a UIView subclass. A UIView defines a rectangle of content and contains some smarts about user input that we’ll deal with later. But hey, that’s visual! What’s it look like?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*T7i86uhZrAnLNuv-c0wXGA.png" /><figcaption>An empty view in Interface Builder.</figcaption></figure><p>Great, we’ve got a view. Our cell is empty and rendered with its default <a href="https://developer.apple.com/reference/uikit/uiview/1622591-backgroundcolor">backgroundColor</a>. We haven’t provided it with any data yet, nor have we exposed any means to give it data. Let’s step up our game and fill the view with UI components. To keep our code snippets focused on the topic at hand, we’ll leave the layout to Interface Builder, but you’re free to set up and lay out the view however you’d like. Here’s the final layout of our cell:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*4gderiW6PM4PYF9Wn__VsA.png" /><figcaption>A designed UITableViewCell in Interface Builder representing a comment posted on a blog.</figcaption></figure><p>Awesome, we’ve got some content! Or… placeholders for content. We’ll need to get the data from the model layer of our application into copies of this cell to display on screen. Before our view model will make sense, we need to define that layer of the application.</p><h4>The Model</h4><p>Let’s look at a simple model type, which mirrors an object returned from a web service. Assume the application has a use for all of this stuff, despite the cell in our example requiring only a subset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5163a45c98d95723d166fd24f9d4d1ea/href">https://medium.com/media/5163a45c98d95723d166fd24f9d4d1ea/href</a></iframe><p>For good measure, here’s the CommentAuthor type referenced in Comment above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa5f145f5714322e2299cf7413aa7eb8/href">https://medium.com/media/aa5f145f5714322e2299cf7413aa7eb8/href</a></iframe><p>Look at all that data! In a real application, there may be even more, like the number of “likes,” more author information, and perhaps storage for multimedia included in a comment. But even so, our cell only needs a subset of this data:</p><ul><li>The author’s name</li><li>The date the comment was posted</li><li>The text of the comment</li><li>Whether the user has edited the comment’s text since posting it</li></ul><p>Although using a value type to model a Comment would save us from our view being able to manipulate any aspect of the application’s model layer, we’re still going to provide the view with only the data it needs. Specifically, our cell subclass will not know about the Comment type. We can accomplish this in a number of different ways including:</p><ol><li>Exposing the UI components on our cell subclass to be populated with the data from the model</li><li>Keeping the UI components private and exposing individual properties for each piece of data that will populate the UI components when set</li><li>Expose a single point of configuration: a view model</li></ol><p>Guess which one I’m going to pick.</p><p>The first option would greatly increase the surface area of our cell’s API. This could lead to heavy configuration being done at the view controller layer, even spreading or duplicating across multiple view controllers if we chose to use this cell in more than one context. We’d expose the ability to fill the labels with <em>any</em> text, modify any stylistic attributes, and easily manipulate layout. <em>No thanks</em>.</p><p>The second option, exposing multiple properties, is better but still not great. We wouldn’t run into any major issues with this approach in our simple example. But, more complex views that expose many configuration properties need to concern themselves with the order in which properties are set and whether some or all have values. This can also lead to multiple layout and drawing passes as each property is configured separately instead of a single pass after configuration.</p><p>The way we use view models aims to solve the problems of the first two approaches. The view only needs to know about the data it displays, and the view exposes a single property, the view model, such that layout and drawing can always happen predictably and completely. So what might this look like?</p><h4>The View and View Model</h4><p>We usually tie a view model type directly to a view type by nesting it. This covers most of our use cases and makes naming trivial thanks to namespacing. Let’s take a look at a snippet of our CommentCell that backs the view we designed earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7dbf374f7382ffc86280db608c1c664/href">https://medium.com/media/f7dbf374f7382ffc86280db608c1c664/href</a></iframe><p>We’ve declared two types: CommentCell which is our UITableViewCell subclass, and CommentCell.ViewModel, the view model that backs this cell type. So far, we have one property declared on CommentCell, which is an instance of CommentCell.ViewModel. This will ultimately become the single point of configuration.</p><p>Notice that in the view model type, most of the properties can easily be mapped from our model later. We don’t want the view controller layer to take on the responsibility of determining how to format and display an author’s name. We’re also packing the author’s name into the same label as the “Edited” demarcation so that it could wrap naturally. This layout is specific to our cell type, and we want to house that logic in our view model. This is an example of <em>presentation logic</em>, which we alluded to earlier. Similarly, the view controller shouldn’t be responsible for determining how to format the date displayed in our view. Rather, we leave that presentation logic to our view model. We’ll put this presentation logic in an extension on the view model type to separate it from its purely model-like properties.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1785cd18e94577ceecaf759deb271c5d/href">https://medium.com/media/1785cd18e94577ceecaf759deb271c5d/href</a></iframe><p>Burying this presentation logic deep inside of aUIViewController subclass would make it more difficult to test. We tend to leave these computed properties exposed in our view models so that they may be more easily accessed in our test target, without the need to instantiate any UIViewControllers or UIViews.</p><p>With the context provided by this example, defining a view model as “a view’s model” begins to make more sense. We store only the properties we need to fill our view with data and we provide operations needed to transform that data into something presentable. Let’s put it to use.</p><h4>A Single Point of Configuration</h4><p>So far we’ve only defined a single property on our cell subclass.</p><pre>var viewModel: ViewModel?</pre><p>However, we’re not making use of this just yet. Since we’re using this property as our single point of configuration for all displayable data, we’ll want changes to this property to update our view. We make use of the computed properties that contain our presentation logic as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de6d652a093fb1ee6d52eb642d168839/href">https://medium.com/media/de6d652a093fb1ee6d52eb642d168839/href</a></iframe><p>We define a single means of configuring our view. This makes configuration safer since it becomes impossible to only specify a subset of exposed properties, relying only on <a href="https://developer.apple.com/reference/uikit/uitableviewcell/1623223-prepareforreuse">prepareForReuse()</a> to reset properties that <em>can</em> be left unset. Rather, we force our controller to set them all at once. Our example cell is relatively simple, but in more complex cases, the order in which UI components are updated may matter more, and by exposing only one property we can ensure the order of updates is consistent and predictable.</p><p>Our UITableViewDataSource implementation will be relatively simple with this approach. It’s straightforward for us to translate to the view model type provided our actual model at this layer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d096e77ee8bd85df5965970d16d4fba/href">https://medium.com/media/1d096e77ee8bd85df5965970d16d4fba/href</a></iframe><p>We’ve plucked a few properties from our model object, and that’s pretty much it. Our view models only hold onto the data they need to fill out the UI components. They also specify a means of translating that data into something to present on screen, as we did in our CommentCell.ViewModel extension.</p><p>If you’re following closely, you’ll notice that our view model specifies its authorImage as a UIImage?, not a URL, so translation from our actual model layer for that particular property doesn’t come quite as easily. You’ll notice that in the snippet above, we’re simply passing nil for authorImage. 🤔 Also, what happens when you tap that reply button on the cell? Well, nothing yet.</p><h4>View models are not a silver bullet against massive view controllers!</h4><p>We’ve housed presentation logic in a location that’s far away from a view controller. However, we’ve kept the responsibilities of our view model simple and well-defined.</p><p>Let’s talk about the elephant in the blog post. Where are those comment author images coming from?</p><h4>Retrieving Data</h4><p>Our view model has a UIImage? property, not a URL. We do this because we <em>do not</em> want our view model to be responsible for downloading images. A view model is a view’s model (remember?), and you wouldn’t put networking code in your traditional model layer, would you? <em>Would you?</em></p><p>(Okay, maybe you did in that one project early in your career, but it eventually bit you, right? And you really hope no one ever sees that model object that retrieves its data from the network on the main thread, parses it, and persists it, right?)</p><p>So why would our view model be responsible for retrieving images from the network? Or from disk? We don’t believe that it should.</p><p>In our example, we passed nil for the authorImage parameter of our view model’s initializer with the expectation that <em>something</em> would come along and set the image once its retrieved from the networking or persistence layer. That’s right, we treat networking and persistence as <em>layers</em> separate from our model, view, view controller, and view model layers. We won’t dive too deeply into this concept as it deserves a blog post of its own, but suffice it to say we inject our view controllers with network and persistence-related dependencies that handle this for us. The important takeaway is that these are <em>not</em> the responsibilities of a view or view model — we’ve seen this architectural mistake all too often. There are other ways of moving this type of work out of view controllers!</p><p>So instead of setting our authorImage to nil, we could have our UITableViewDataSource ask the persistence layer of our application to retrieve an image for a particular URL. This would change our configuration from above to something like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e7467e0dc09a18087042caae755b672/href">https://medium.com/media/2e7467e0dc09a18087042caae755b672/href</a></iframe><p>The method image(for:) in this example would return an optional UIImage since there’s no guarantee that we’d actually have that image yet. We’d defer downloading of the image, prioritizing display of the data that we already have. We could kick off the downloading of images in <a href="https://developer.apple.com/reference/uikit/uitableviewdelegate/1614883-tableview">tableView(_:willDisplay:forRowAt:)</a> for the cases in which we don’t have them on disk, or perhaps when our local cache has expired and we want to update an existing image already stored on disk. Once we have this image, say, in the completion handler of a network request method, we can simply set it on the cell’s view model as follows.</p><pre>cell.viewModel?.authorImage = imageFromNetwork</pre><p>Now you might notice that we’ve actually left the authorImage property on CommentCell.ViewModel a var as opposed to a let. This is great because it allows us to change the value of a displayable property that we expect to change, but setting it still triggers our viewModel property’s didSet relieving us from any concerns about the order in which UI components are configured. Despite the fact that we’re updating a single property, we still hit our single point of configuration. (Should I create a <a href="https://smilesoftware.com/textexpander">TextExpander</a> snippet for that phrase?)</p><p>To belabor the point, our view models <em>do not</em> touch the networking layer and <em>do not</em> touch the persistence layer of our application.</p><h4>Delegating Action</h4><p>Typically, the handling of tapping a cell can be performed by either a storyboard segue or in the UITableViewDelegate method <a href="https://developer.apple.com/reference/uikit/uitableviewdelegate/1614877-tableview">tableView(_:didSelectRowAt:)</a>, commonly implemented by the UIViewController or type that is used by the UIViewController. But notice that in addition to being able to tap the cell, we’ve also got a reply button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*lLQpCWe4Xt1iDrmzSgjGug.png" /></figure><p>So, does the view model take part in this interaction? No. We like to follow the same pattern as cell selection, in that it can done via a storyboard segue if we’ve specified our layout in prototype cells in a storyboard, or by allowing the UIViewController to handle the interaction. We do not want the view model or view to own any of this responsibility. Let’s walk through the non-storyboard approach.</p><p>First, we need to prepare our cell class to be able to intercept the button tap. We’re not going to carry out the button’s action, but rather provide a means for our view controller to do so. We declare a closure to be set externally on our cell and a corresponding action method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ee3cd7c4216e3bc19995742166e98ec/href">https://medium.com/media/3ee3cd7c4216e3bc19995742166e98ec/href</a></iframe><p>Whether you use an @IBAction or register a target/action using <a href="https://developer.apple.com/reference/uikit/uicontrol/1618259-addtarget">addTarget(_:action:for:)</a>, you’ll simply have to call the closure when the button is tapped.</p><p>All we have left to carry out the action of tapping a CommentCell’s reply button is to set the replyButtonTapHandler. We do this in the same location that we configured the viewModel property discussed earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4159bb42c1106d222401764f72015228/href">https://medium.com/media/4159bb42c1106d222401764f72015228/href</a></iframe><p>So, the single point of configuration is for all display-related data. We separate actions into their own properties, as it’s not the view model’s responsibility to carry out the handling of user input. These actions could also be handled via delegation, but we prefer closure properties for this type of interaction.</p><h3>Be a (View) Model Citizen</h3><p>Looking back at our simplest definition — a view model is <em>a view’s model </em>— the example we’ve illustrated matches this. It also helps to draw very clear barriers about what a view model is <em>not</em>. With this knowledge, we can lighten some of the load on our view controllers while preventing our view models from taking on too much responsibility.</p><p>For more on view models, our friends <a href="https://twitter.com/cdzombak">Chris Dzombak</a> and <a href="https://twitter.com/khanlou">Soroush Khanlou</a> spent episodes <a href="https://fatalerror.fm/episodes/2016/8/13/2-view-models">2</a> and <a href="https://fatalerror.fm/episodes/2016/8/29/3-view-models-again">3</a> of their podcast <a href="https://fatalerror.fm/">Fatal Error</a> on the topic, which include quality coverage of the problems they can solve, the MVVM design pattern as it relates to iOS development, pitfalls, and more. Check out the show notes for these episodes which include links to many more relevant resources. See also Andy Matuschak’s 2015 NSSpain talk <a href="https://vimeo.com/140037432"><em>Let’s Play: Refactor the Mega-Controller</em></a><em>.</em></p><h3>What’s Next?</h3><p>Of course, there’s still more to cover. The approach that we outlined here doesn’t apply to views that are reusable in different scenarios. Our example included a cell for a blog post comment, specifically, but it’s certainly possible that an app would have much simpler cells that could be reused in multiple contexts. In these circumstances, we make use of view model protocols to share common layout behavior for views with varying purposes. We’ll explore this approach in our next post on view models. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bb1d0675038" width="1" height="1" alt=""><hr><p><a href="https://medium.com/inside-lickability/our-view-on-view-models-4bb1d0675038">Our View on View Models</a> was originally published in <a href="https://medium.com/inside-lickability">Lickability Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Escape from Times Square]]></title>
            <link>https://medium.com/inside-lickability/escape-from-times-square-fc6c6ae14c41?source=rss-ec13c225c26f------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc6c6ae14c41</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[new-york]]></category>
            <dc:creator><![CDATA[Michael Liberatore]]></dc:creator>
            <pubDate>Mon, 14 Nov 2016 21:23:33 GMT</pubDate>
            <atom:updated>2016-11-14T21:23:33.594Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>Matt</em></strong>: “Can you get the Uber?”</blockquote><blockquote><strong><em>Andrew</em></strong>: “Sure… aghh, never mind…”</blockquote><blockquote><strong><em>Matt</em></strong>: “Times Square bug?”</blockquote><blockquote><strong><em>Andrew</em></strong>: “Yep.”</blockquote><blockquote><strong><em>Matt</em></strong>: “I can’t believe they haven’t fixed this by now.”</blockquote><blockquote><strong><em>Andrew</em></strong>: “How do I work around it again?”</blockquote><blockquote><strong><em>Matt</em></strong>: “Restart your phone.”</blockquote><p>For most of 2016, the Lickability team was plagued with an iOS issue causing all of our devices to report their current locations to be in the center of Times Square, <a href="https://www.quora.com/Why-do-New-Yorkers-dislike-Times-Square">New Yorkers’ favorite spot</a> in the city.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yDco88_HoIJj26iaQn14Jw.png" /><figcaption><em>Not again…</em></figcaption></figure><p>The only consistent workaround was to restart our phones. After conferring with other iOS developers outside of the company, we believed it to be a widespread problem. Researching online left us wondering how this hadn’t been reported to Apple or heavily discussed on forums.</p><p>As time went on, frustration grew. Location sharing in Messages and Find My Friends would suggest we spent most of our days waiting in line for last minute discounts on Broadway tickets at <a href="https://www.tdf.org/nyc/7/TKTS-ticket-booths">TKTS</a>. Rather than exploring some of the best restaurants in the world, friends assumed we were waiting patiently outside of Olive Garden with restaurant buzzer in hand. Transit and walking directions were misleading, ordering food from <a href="https://www.seamless.com/">Seamless</a> became a hassle, and I frequently found myself shouting “No Foursquare, I’m <em>not</em> back at <a href="https://foursquare.com/v/mms-world/4a065273f964a520ec721fe3">M&amp;M’s world</a>!” <a href="https://developer.apple.com/reference/corelocation">Core Location</a> had gone from helpful to irritating. After months of dealing with this issue, and continuing to reach out to other developers in desperation, we were reminded of a debugging feature of Xcode to simulate location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*W1dTjS9ziEiOanU4FZmegw.png" /><figcaption>Edit Scheme… → Run → Options</figcaption></figure><p>This was a great starting point. The only issue was, none of us had used this debugging tool, and it was still happening to us. Which brings us to our next friend:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*V1UapF3PGNMiVH5Cm15mQw.png" /></figure><p>At some point, one of us had pushed a change to a shared run scheme on a client project to simulate the device or simulator location to “New York, NY”. Those “other iOS developers” who shared in our frustration early on were employees of the client. We had done this to ourselves! Merely running the app using the default scheme in the Xcode project would spread the problem to a device, persisting long after the work day had ended. Finally, Lickability could breathe a collective sigh of relief, share a few laughs, and learn the lesson of not carefully reviewing changes inside of xcshareddata.</p><p><em>Special thanks to </em><a href="https://twitter.com/masterjeef"><em>Jeff Forbes</em></a><em> for pointing us in the right direction to help us escape from Times Square.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc6c6ae14c41" width="1" height="1" alt=""><hr><p><a href="https://medium.com/inside-lickability/escape-from-times-square-fc6c6ae14c41">Escape from Times Square</a> was originally published in <a href="https://medium.com/inside-lickability">Lickability Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>