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.

I received the following in response to my letter about the basic income pilot project that the Ontario government is cancelling. I know this is a form letter, but it doesn’t address the pilot project at all, and talks about the 1.5% increase in “support rates” as if this is up from 0%, not down from the previous government’s planned 3%.

None of this is surprising, just disappointing.

Thanks for getting in touch with me to share your views about our plan to reform social assistance in Ontario. I appreciate hearing from you.

Our social assistance programs are an important part of the safety net designed to assist our most vulnerable people. This is an important responsibility, and one we take seriously. Upon assuming government and reviewing the system we inherited, it quickly became apparent that the status quo was not working for people in need. Instead of helping people get their lives back on track, the old system left too many people trapped in a cycle they could not break out of.

We’ve set an accelerated 100 day deadline to develop and announce a sustainable social assistance program that focuses on helping people lift themselves out of poverty. Our goal is to do more to help people get off social assistance, find good jobs and get their lives back and track. While we are doing this work, we will be providing Ontario Works and Ontario Disability Support Program recipients with an across-the-board 1.5 per cent increase in support rates to help them with a higher cost of living. These efforts will go hand-in-hand with our previous commitments to reduce gas prices by 10 cents per litre, lower hydro rates, and provide targeted tax relief for working parents and minimum wage earners, all of which will provide focused benefits to lower income families.

Social assistance will always be about compassion for people in need, but it must also be about lifting people up and helping them get their lives back on track through more jobs, more opportunities and more hope. Tackling the serious issues facing our social assistance system isn’t an easy thing to do. But it’s the right thing to do, and we’ll get this right.

Thanks again for contacting me.

Doug Ford Premier of Ontario

Ontario’s previous provincial government started a Basic Income pilot project. It was to last three years, and has just been cancelled by our new Progressive Conservative government, on the grounds that it’s “not sustainable”.

We should all be writing our representatives about this. Here’s my letter to Premier Ford and Children, Community and Social Services Minister MacLeod. Feel free to make suggestions, or copy it for your own letters.

Dear Premier Ford and Minister MacLeod,

I’m writing to express my extreme disappointment in your decision to discontinue the basic income pilot. Basic income may be a powerful tool to spend less money to help more people. (It may not be; we don’t know yet.) Similar programmes have been successful elsewhere and I was pleased to learn that the previous Ontario government wanted to study it too.

Cancelling the programme before it is complete is a waste of money and pre-supposes its results. Moreover, the reason given by Minister MacLeod – that it isn’t sustainable – is ridiculous in the face of it. By nature of its pilot status – with defined budget and, more importantly, an end date just two years from now – sustainability is not an issue. In fact, its purpose is specifically to study the sustainability (and effectiveness) of a basic income programme.

Do not make excuses. Your given reason for cancelling the basic income pilot is nonsensical, so we are forced to assume you are cancelling it simply because you don’t like it; that it doesn’t fit with your conservative ideology. Either say so, or wait until the pilot is complete so you have the necessary information to decide whether to implement a basic income programme providence-wide. (Preferably the latter.)

Jason Sadler