BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Facebook AsyncDisplayKit Touts Smooth Asynchronous UI for iOS Apps

Facebook AsyncDisplayKit Touts Smooth Asynchronous UI for iOS Apps

This item in japanese

Bookmarks

Facebook has open-sourced its AsyncDisplayKit, a framework originally built for Facebook's Paper app that promises to make it easier to keep apps smooth and responsive even on older devices.

The main benefit that AsynDisplayKit should provide, according to Facebook, is the ability to use image and text views and to create and render complex view hierarchies completely off the main thread. As it is known, moving work off the main thread is key to ensure user interactions can be promptly handled. Furthermore, moving work to a background thread also makes it possible to take advantage of multicore architectures.

iOS provides various ways to do that, such as Grand Central Dispatch or NSOperations, but UIKit, the main workhorse behind iOS UI, remains mostly only safe to be used on the main thread.

Ensuring that the user experience remains smooth, means meeting a time constraint of only a few milliseconds for each UI update. This can be especially hard to do with main-thread-only UIKit views like UIImageView and UITextView, which can take tens to hundreds of milliseconds to size and display themselves due the complexity involved in handling graphics and text.

The solution to this is usually using lower-level frameworks to handle image and text display, such as CoreGraphics and CoreText, which means working with a lower-level, non-ARC C-API.

AsynDisplayKit builds on UIKit, Core Animation, and Core Text to offer a drop-in replacement for UIImageView and UITextView, namely ASImageNode and ASTextNode. Furthermore, a ASTableView class is also available that can asynchronously preload cell nodes without blocking the main thread.

How it works

As mentioned, AsynDisplayKit hierarchies can be initialized and laid out on background threads:

dispatch_async(_backgroundQueue, ^{
  ASTextNode *node = [[ASTextNode alloc] init];
  node.attributedString = [[NSAttributedString alloc] initWithString:@"hello!"
                                                          attributes:nil];
  [node measure:CGSizeMake(screenWidth, FLT_MAX)];
  node.frame = (CGRect){ CGPointZero, node.calculatedSize };

  // self.view isn't a node, so we can only use it on the main thread
  dispatch_sync(dispatch_get_main_queue(), ^{
    [self.view addSubview:node.view];
  });
});

This will not block the main thread while creating the ASTextNode object. You can also see that the ASTextNode object is finally added, on the main thread, to a common UIView, showing interoperability between AsynDisplayKit objects and UIKit objects.

Another interesting feature offered by AsynDisplayKit is the possibility to convert a view hierarchy into a layer hierachy by setting the layerBacked property. This should also translate into better performance, without having to explicitly port view-based code to the Core Animation API.

AsyncDisplayKit is the second framework that Facebook open-sources after Pop, which was already covered on InfoQ.

Rate this Article

Adoption
Style

BT