Local Storage (Day 10)

Local storage can be needed for a number of things, user preferences, security tokens or caching downloaded data. What I will go through in this post is where and some ways to retain information locally on the device.

The common ones are:

  • Keychain / KeyStore
  • NSUserDefaults / SharedPreferences
  • Internal (Application) Storage
  • External Storage
  • SQLite (though really just a file in storage)

StorageOptions

For slightly more secure storage choose either

  • KeyStore
  • KeyChain
  • IsolatedStorage
  • ApplicationDataContainer

Your next best option is to always choose something in Internal Storage. Internal Storage means only the mobile app can access this data and not any other application. External Storage is available to any app on the phone and is not secure.

For secure storage have a look at Secure Storage on XLabs.

For internal settings storage have a look at the Settings Plugin for Xamarin. Its great for storing persistent non sensitive data.

SQLite

If you need a more traditional relational database to store information the best option is SQLite. It is very popular and heavily used.

Download SQLite.Net.Async PCL and it will download the other dependencies for you. This gives you SQLite capabilities and you can see some examples on their GitHub page.

However if you want to use it in Xamarin Forms you will need to use a bit of dependency injection.

Windows Phone

iOS

Android

SQLCipher

If you need your local SQLite database to be secured you can use SQLCipher. Its free if you have a Xamarin Enterprise License otherwise its a paid product. It encrypts the database locally so that even if the phone is stolen the database will still be secure.

Entity Framework

Wouldn’t it be great. Apologies to disappoint here but it is available for UWP but there has been no port for MonoAndroid or MonoTouch and I can not find anything on any roadmap that they will even be trying. I was really hoping as I have a lot of past EF experience, even EF7 with ASP.NET5 and I was really hoping to see it in Xamarin. Maybe one day we will but I wouldn’t bank any upcoming or current project on it.

The only option of anything above SQLite is SQLite Extensions.

Couchbase (NoSQL)

If you are looking for a NoSQL database, Couchbase Lite is a popular option. You can download the NuGet package and read the Getting Started guide to get up and running really fast.

<< Security And Authentication (Day 9) || Styling UI/UX (Day 11) >>

14 Days To Building An Enteprise Quality Xamarin Forms App

Microsoft MVP | Xamarin Certified Developer |
Exrin Creator | Xamarin Forms Developer | Melbourne, Australia

Related Posts