BT

Facebook AsyncDisplayKit Touts Smooth Asynchronous UI for iOS Apps

| by Sergio De Simone Follow 14 Followers on Oct 17, 2014. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT