Data Storage in Xamarin tvOS
Data Storage in Xamarin tvOS
20 August 2020
Introduction to tvOS
Apple TV is a digital media player used to play digital content such as music, video and can also be used to play games with internet connectivity. It was released by Apple Inc. on 9 Jan 2007 and currently, it has the latest version as Apple TV 4K (5th generation). It has the operating system named tvOS which has 13.3.1, the latest stable version.
Xamarin for tvOS
In Xamarin, with the C# codebase, we can develop cross-platform as well as native level apps for mobile and tvOS apps can be developed using Xamarin on the native level.
Limitations in Apple TV
Apple TV has many amazing features but, as the coin has two sides, it also has lots of limitations. Some common limitations are as follows:
- No Webview: There is no passive viewing on Apple TV. We can only navigate through the apps.
- Limited UI elements: Apple TV supports the limited bunch of UI components provided by UIKit.
- No persistent local storage: Apple TV does not have persistent storage.
- App size limit: Apple TV limits apps size to 200 MB.
So, how to store data in the Xamarin tvOS apps?
There are some ways to provide data storage in Apple TV as given below:
- Preferences (Xamarin.Essentials)
If we require resources beyond the size limit then we can pack and load using On-Demand Resources, but it has a limit of 2GB.
NSUserDefaults is used for very small-sized data to store persistently. But for each application, it has a limit of 500KB of data. It is used to store key-value pairs that can be accessed at the app launch and can be modified at runtime.
We can understand it, with the help of following example:
Here we are storing key-value pairs where “KeyName” is the key and “valueName” is the string value that we have to store and access later app sessions.
NSUserDefaults.StandardUserDefaults.SetString(valueName, "KeyName"); NSUserDefaults.StandardUserDefaults.Synchronize();
valueName = NSUserDefaults.StandardUserDefaults.StringForKey("KeyName");
The common data types supported by NSUserDefault are:
After uninstalling the application, NSUserDefault values will automatically be cleared out.
Preferences are used to store tiny bits of data in key-value pairs and it can be used after adding the reference of Xamarin.Essentials library.
var myValue = Preferences.Get("keyName", "defaultValue");
Preferences.Remove("keyName"); (remove single preference) Preferences.Clear(); (remove all preferences)
The common data types supported by preferences are:
After uninstalling the application all preferences will be automatically cleared out.
If the application requires a large amount of data to store and access, then iCould will be helpful. The iCloud-based data storage system has two types:
- iCloud Key-Value Storage (KVS):
KVS is used to store a small amount of data which is less than 1MB. The data stored in KVS is accessible in all the devices for the same user.
CloudKit is used to store a large amount of data which is greater than 1MB. The data stored in it can be accessible to all the users of the app and can also be restricted to access to a particular user.