Each year after Apple’s World Wide Developer Conference we switch most of our focus onto a bigger update to our Buffer iOS app to build out support for the newest iOS features and changes.
This year was no different, but this time we also focused a bunch of time on performance and future proofing.
New version of iOS = new changes to Buffer iOS app
During the time after WWDC, we typically will run through the app and adjust our minimum target to the previous iOS release. In this case, it meant adjusting the minimum to iOS 9, dropping support for iOS 8 devices. This allows us to clean up the app and remove any old iOS 8 code that is no longer needed for backwards compatibility, and it keeps our tech debt down each year.
One area we wanted to adjust and improve with v6.0 of the Buffer iOS app was with our update tables, specifically adding the ability for us to have more flexibility in terms of layout in the future. We’ve made use of UITableViews in the app since 2011. They are great for lists of content, but we wanted to explore displaying content in grids for future updates to v6.
Our goal: 60 frames per second for smooth scrolling
We’ve always had some struggles with performance around our update tables when trying to achieve the magical 60 frames per second needed to have smooth scrolling. This can be a difficult mark to reach, as even though iPhones become more powerful each year, UIKit is still constrained to the main thread as it was designed and developed at a time when smartphones were single core devices.
That leaves developers with around 16 milliseconds to execute all layout and drawing code before the user starts experiencing noticeable frame drops. With system overhead, sometimes that number becomes a staggering 10 milliseconds.
As our UI becomes more robust and varied, the challenge grows each year to maintain (or, more ideally, gain) performance. This technical challenge grows exponentially when you consider how to smoothly retrieve updates from the server while users scroll to avoid the road block of a “Loading” indicator or spinner.
Our cells are fairly complicated since they need to support UI for each network we support along with the varying heights of social media updates that users create. So, we were excited when Apple announced some new additions to UICollectionViews this year to improve performance. Our hope was to hit a steady 60 frames when scrolling in the most process-intensive scenarios in our tables, up from our previous mark of ~55 frames.
We started exploring how to use UICollectionViews within the app, but we hit a few roadblocks with achieving a layout similar to our current UITableViews along with a few challenges with the new additions for auto sizing collection cells. We were still seeing our scroll indicators jump up and down as iOS calculated the heights; we also saw this in our v5.0 app, which tried to pre-calculate heights when possible. Furthering the issue is that the convenience of Auto Layout comes with the cost of heavy calculation, again happening on the main thread.
Our strategy: Using Facebook’s AsyncDisplayKit
We had heard of AsyncDisplayKit (ASDK) from Facebook a while back but hadn’t explored it yet. Facebook custom built ASDK back in October 2014 for their gesture- and animation-featured app, Paper. They created it as a way to improve the smoothness of apps by helping them maintain the 60 frames per second by rendering views off of the main thread via a thread-safe abstraction of UIView. In addition, ASDK also has an advanced API, called “Intelligent Preloading”, which handles network calls and reloading efficiently in scenarios where users scroll through a “list” of content that requires responses from a server.
It crossed our minds to use this time to check it out and within a few hours we had a branch of v6.0 running with text only updates displaying with no jumping of our scroll indicators.
As AsyncDisplayKit handles the rendering of the cells, they get returned at the correct height and no implementation of heightForRowAtIndexPath: or estimatedHeightForRowAtIndexPath: are needed. With a proof of concept up, we were seeing a smooth 60 frames on an iPhone 5C — easily outpacing our previous implementation running on side-by-side on an iPhone 6 Plus.
As AsyncDisplayKit uses “Nodes” for building out UI, we took on the task of re-writing our update cells to be ASCellNodes, spending the time to also tidy up the structure of our cells by abstracting different elements off to new classes, going from a rather alarming 2800 lines down to 945 lines. ASCellNodes also have the added benefit of being compatible with both Collection & Table views allowing us the flexibility in layout that we were after in our refactor for future features.
Layout in ASDK is based on the CSS FlexBox Model which is a bit of a departure from our existing Auto Layout code, but it’s fairly easy to pick up with LayoutSpecs that allow you to Inset, Overlay, Center, Stack & more. We were able to quickly build out the layout for our update cells and have them looking even tidier than they were with Auto Layout along with support for orientation changes and constraining the width for readable layout guides on iPad.
Since the introduction of AsyncDisplayKit, Pinterest has teamed up with Facebook and they’re both working on v2.0 of AsyncDisplayKit. We’ve always been impressed with the performance of both Facebook and Pinterest’s iOS apps. It’s really impressive when swiping through pins rapidly in the Pinterest app, and there are hardly ever any loading screens. We’re super excited to explore using ASDK in other areas of the Buffer iOS app, as we’ve been really impressed with the performance improvements we’ve seen.
Here’s a quick video taken during the development showing the v5.6 queue, with the newly recoded version on the right. You’ll notice less skipped frames along with less scroll indicator jumping with iOS calculating the cell heights.
What’s next + your thoughts
We hope to share some open source projects soon with ASDK examples to help others explore using ASDK in their own apps. One of the first being the code used for reordering updates within the Buffer iOS app.
If you’ve had the chance to use ASDK on a project, we’d love to hear your experience. If you have any tips or questions about smooth scrolling, it’d be great to chat in the comments.