Utilizing On-Demand Resources
On-Demand Resources, introduced by Apple at WWDC 2015 for iOS 9, allows you to separate specific assets of your app from the main bundle and dynamically download them only when needed, allowing better utilization of storage space. 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 executable code.
The results 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 only needs the resources associated with the current and next levels.
- Remote storage of rarely used resources. The app has resources that are used infrequently, and 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, if 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 almost any Apple TV apps since a tvOS bundle has a hard limit of 200MB. Recently, at the Apple TV Tech Talks in NYC, Apple gave some tips on how to take advantage of On-Demand Resources.
You can mark assets that are not required 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.
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.
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.
If you want more information about On-Demand Resources and how to set everything in your project, I highly recommend reading Apple’s documentation.
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 any app that needs to store a lot of assets and allows the developer to more properly manage the user’s device storage.