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.

Data Storage

So, how to store data in the Xamarin tvOS apps?

There are some ways to provide data storage in Apple TV as given below:

  • NSUserDefaults
  • Preferences (Xamarin.Essentials)
  • iCloud

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:

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.

Create:

NSUserDefaults.StandardUserDefaults.SetString(valueName, "KeyName");               
NSUserDefaults.StandardUserDefaults.Synchronize();

Access: 

valueName = NSUserDefaults.StandardUserDefaults.StringForKey("KeyName");

Remove:

NSUserDefaults.StandardUserDefaults.RemoveObject("KeyName");

The common data types supported by NSUserDefault are:

  • string
  • integer
  • float
  • boolean
  • URLs

After uninstalling the application, NSUserDefault values will automatically be cleared out.

Preferences (Xamarin.Essentials):

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.

Create:

Preferences.Set("keyName", "valueName");

Access:

var myValue = Preferences.Get("keyName", "defaultValue");

Remove:

Preferences.Remove("keyName"); (remove single preference)
Preferences.Clear(); (remove all preferences)

The common data types supported by preferences are:

  • bool
  • double
  • int
  • float
  • long
  • string
  • DateTime

After uninstalling the application all preferences will be automatically cleared out.

iCloud:

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:
    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.

search
Blog Categories
Request a quote