micro

I’m listening to music from the original Mass Effect and I am having some very intense nostalgia.

micro

Oh man. Pixar’s Coco. They know how to turn on the waterworks, right on cue.

TLDR: Table view (and collection view) data source should be backed by a snapshot of your data store; not a real-time view of it.

I attended last week’s iOS KW meetup, which was a talk by Chris Liscio about Collection View Controllers on iOS and macOS. The talk was good, featuring a lot of interesting details of how Chris implemented the main view in his music app, Capo. But what was really interesting is what I learned in chatting with him afterwards. I’ve been doing iOS development for more than 6 years, and this finally flipped a switch in my brain about why managing updates in table views (and collection views) was always so difficult for me.

Animating updates to a UITableView’s contents is ostensibly pretty easy—you get notifications about the contents of your data store, and you tell your table view which rows were added, removed, or updated. In order to have things animate all together nicely, you do this in a transaction:

tableView.performBatchUpdates { 
	tableView.insertRows(at: [IndexPath(row: 3, section: 1)], animation: .automatic) 
	tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], animation: .automatic)
}

However, there are some arcane rules about the order in which these updates are processed—not in the order in which you submit them, but all reloads first, then deletes, then insertions. In other words, reloads and deletes are processed relative to the state of the table before you begin making any updates, and insertions are processed relative to the state of the table once deletions have completed.

In the past, when I had to make updates based on a completed network request, for instance, I would call tableView.beginUpdates(), then respond to KVO notifications by calling insertRows() and its friends as the network response handler added, updated, and removed items in the data store, and then call endUpdates() when processing was complete. Since the updates to the data store could happen in any order, it was extremely difficult to try to re-order everything so that it would make sense in the order that UITableView performs its updates. Crashes occurred often. This was also a source of high coupling between classes that should have little or now knowledge of each other.

It turns out the answer is very simple. As Chris described, your data source should represent a snapshot of your data, not a real-time view of it. After changes have finished, you move your snapshot forward, compute a diff, and send the necessary insert / delete / refresh messages to the table view. This is apparently what NSFetchedResultsController does under the covers, and other technologies such as Realm allow you to either update your thread’s snapshot manually or at the beginning of a runloop.

The key is that any changes that occur in your UITableViewDataSource should only happen during a table view updates transaction. If your data source reflects the real-time state of your backing store, values might have already changed by the time you’re responding to a them, which can get you into trouble with the frameworks.

So, lesson learned, 6+ years later: `UITableViewDataSource` shouldn’t represent live data. Move your snapshots forward during your table view’s update transactions. Have fewer crashes.


The Sublime Text folks made a git client.

Today, I’d like to introduce Sublime Merge. It combines the UI engine of Sublime Text, with a from-scratch implementation of Git. The result is, to us at least, something pretty special.

Oh wow. Oh wow.

We have a custom implementation of Git for reading repositories, which drives a lot of our high performance functionality. However we defer to Git itself for operations that mutate the repository (Staging, Committing, Checking out branches, etc).

Sign me up!


Apple has differentiated its new iPhones in a super frustrating way, probably in an effort to drive up average selling prices.

  • iPhone XR: phenomenal performance to price ratio. Offers 128 GB storage. Awesome colour choices. Aluminum instead of steel. Inferior but still great screen. No 3D Touch. Only one camera.
  • iPhone XS and XS Max: awesome screen. Two cameras. Doesn’t offer 128 GB – only 64, 256, and 512.

I am currently using 78 GB of storage on my iPhone 7+. Way too low to justify 256 GB for an additional $210, but way too high to try to fit it all in a 64 GB phone. And I really want the optical zoom capability of the dual camera setup – something I use all the time on my current phone.

So either I can pay $1099 and settle for a single camera, or pay a whopping $1589 to get all the things I actually want, plus a bunch of things I don’t care about.

I’m willing to bet a lot of other people are in the same camp.

Furthermore, Apple is selling these new devices at CAD$1.40 to the US Dollar, instead of the actual current exchange rate, CAD$1.30. They usually treat us Canadians pretty fairly, but this is awful.