Apple TV Tech Talk – On-Demand Resources

This year I had the chance to participate to the Apple TV Tech Talk in New York. This blog post is the first of a series of blog posts on interesting subjects Apple shared with us about tvOS but also iOS.

On-Demand Resources were introduced by Apple at WWDC 2015 for iOS 9. It allows your app to separate specific assets of your app from the main bundle and dynamically download them only when needed. All assets are hosted on the App Store so you don’t need to worry about it. The resources can be mostly anything, with the exception of for executable code.

The benefits are incredibly beneficial as described by Apple in their documentation:

  • Smaller app size. The size of the app bundle downloaded by the user is smaller, resulting in faster downloads and more storage room on the device.
  • Lazy loading of app resources. The app has resources that are used only in certain states. The resources are requested when the app is likely to enter the appropriate state. For example, in a game with many levels, the user needs only the resources associated with the current and next levels.
  • Remote storage of rarely used resources. The app has resources that are used infrequently. The resources are requested as they are needed. For example, an app tutorial is usually shown once after the app is opened for the first time, and may never be used again. The app requests the tutorial on first launch, and then requests the tutorial only when needed or when new features are added.
  • Remote storage of in-app purchase resources. The app offers in-app purchases that includes additional resources. The resources for purchased modules are requested by the app after it is launched. For example, a user purchases the SuperGeeky emoticon pack in a keyboard app. The app requests the pack after it finishes launching.

On-Demand Resources are very powerful for video games but also for Apple TV apps since a tvOS bundle has a hard limit of 200MB. In this post I’ll give you some tips that Apple shared during the Apple TV Tech Talk on how to take advantage of On-Demand Resources.

Purge assets

You can mark assets that are not required anymore as unused so the system knows what to remove first. If a file is marked as unused you should assume that this asset is not available anymore, so you may have to download it again.

You can also set a priority order to purge your unused assets. You can assign a value from 0 (normal) to 1 (highest), and the system will determine what assets need to be removed first.

Tagging

You can tag your files to categorize them. For example in a video game, “Level 1” and “Level 2” are common tags that you can use. Remember that you can tag files or folders. You can assign a file multiple tags too, in the event a file is reused in various places.

You can use special tag categories to help the system prioritize the download:

Initial install tags

The assets tagged with the initial install tag are downloaded alongside the app installation. You can tag up to 2GB of assets, and they are included with the total size of your app in the App Store. Initial install tags are very handy for downloading the more immediate assets of your app – for example, onboarding screen assets – and avoiding a loading screen at first launch just to download them. By using them you will greatly improve the first launch experience of your users.

Prefetch tags

Downloaded immediately after installation. You can tag up to 4GB minus the size of the initial install-tag assets. This is useful when you want to download assets that you will need soon but not for the app launch – think, for instance, of the first level of your game.

Flow

Here is the basic flow to get data through On-Demand Resources:

  • Initialize your assets with a set of tags
  • Begin a request to download the assets
  • Tell the system when you’re finished
  • Track the progress of your request. Apple recommends that you do not show a progress indicator to your users while fetching resources; instead, you should try to download the assets in the background so the data will be ready when needed
  • Control the progress
  • Pause, resume and cancel when needed

Loading priority

You can set a relative priority of simultaneous requests to help the system prioritize the download. The values goes from 0 (normal) to 1 (urgent). You can also further mark an asset as incredibly urgent priority when the user is blocked waiting for a request to complete; doing so will suspend all the non-urgent requests and download the urgent assets regardless of CPU usage.

Conditional requests

You can check the availability of the various on-demand resources in your app as well. This can be useful to keep track of an inventory of assets. Remember to cancel every request that is not needed!

Conclusion

As you can see On-Demand Resources is a great way to deal with a large amount of assets and dynamically control them based on the user’s need. It is very useful for video game apps but not only, some regular apps also store a lot of assets and by using On-Demand Resources you can save them some storage on their devices and some time when they download your app.

If you want more information about On-Demand Resources and how to set everything in your project, I highly recommend reading Apple’s documentation.