Think Big, Act Small, Fail Fast and Learn Rapidly


Xamarin Diaries – Offline by default (Part II)

Xamarin Diaries

Hi All!

In this new chapter we’ll continue talking about offline functionality in Xamarin.Forms apps. This time I want to cover the possibilities we have to store our data locally.

If you remember from the previous post  – Xamarin Diaries – Offline by default (Part I) -, when we retrieve data from backend (typically in JSON format) the first thing we do is to synchronize our local data. Next time we run the same query this data can be directly retrieve from local storage (specially if we fall into offline conditions).

For this local storage we can choose between two different possibilities:

  • SQLite
  • Text JSON files

Let’s take a deeper look into the two options:


This is the standard solution and it’s probably the solution I would go in most cases. Basically you will need to add some of the SQLite plugins to start. If we take a look to nuget there are two main plugins according to number of downloads:

The second options is no longer maintained and it is in fact a fork of the first one so the first option is my recommendation.

As an important point, sqlite-net-pcl last updates already include references to sqlitepclraw green to avoid problems with restrictions added with Android N. If you go with the second option you will have to add them manually.


Performance: SQLite performance is better than reading and overwriting files. For little applications it is probably not a problem, but when applications grow it is better to have indexes and transactions on your side.

Security: Although sqlite databases are just a db3 file stored inside Personal folder (so they are more or less the same than json files), they allow developers to add security layers. For example you have raw-cipher for encryption

Standardization: standardized solutions are always better when it comes to business. Maintainability and scalability are linked to standardization.


Ease of implementation: you will find that multi level hierarchies in the model of your application are not well supported in SQLite, and so do one-to-many and many-to-many relationships. You can use the ORM layer included in sqlite-net or add some other plugins, but in my experience, I prefer to do it myself by applying manual conversions. For example, something like:

Can be converted to:

public class IncidentTypeItem
public string Id { get; set; }
public string DescriptionEs { get; set; }
public string DescriptionEn { get; set; }
public string DescriptionPt { get; set; }
public string DescriptionCa { get; set; }


With the following converter

public static class IncidentTypeToIncidentTypeItem
public static IncidentTypeItem Convert(IncidentType incident)
var item = new IncidentTypeItem
Id = incident.Id,
DescriptionEs = incident.Description.Spanish,
DescriptionEn = incident.Description.English,
DescriptionPt = incident.Description.Portuguese,
DescriptionCa = incident.Description.Catalan
return item;
public static IncidentType Unconvert(IncidentTypeItem item)
var incident = new IncidentType
Id = item.Id,
Description = new Description
Spanish = item.DescriptionEs,
English = item.DescriptionEn,
Portuguese = item.DescriptionPt,
Catalan = item.DescriptionCa
return incident;


JSON Files

This option could sound as a no so beautiful solution but in some cases I found it very useful in situations you just need something quick that could work as cache.

For this solution you will need a connector so that you can manipulate files natively in Android and iOS from PCL Project. I have leave it in github if case you want to take a look to it:

When working with JSON files as cache you will basically do something like this:

Save items to local storage:

var itemsAsJson = JsonConvert.SerializeObject(Items, Formatting.Indented);
DependencyService.Get<IFiles>().SaveText(string.Format("items_{0}.json", AuthManager.Instance.User.Id), itemsAsJson);


Retrieve items from local storage:

var jsonText = DependencyService.Get<IFiles>().LoadText(string.Format(“items_{0}.json", AuthManager.Instance.User.Id));
var jsonResponse = JsonConvert.DeserializeObject<List<Item>>(jsonText);
if (jsonResponse != null && jsonResponse.Count > 0)
Items = jsonResponse;


Special mention to Newtonsoft.Json ( as library to manipulate json objects.


Ease of implementation: There is no need to create conversor or anything, you just read from file and apply same json parse than in for your model.

Performance: For small applications is it just fast enough. I wouldn’t use it if the app grows.


Security:  No security for files, they are plain text stored in Personal folder. You could apply some cipher to encrypt the content of the files, but I just find it too difficult for a solution only thought for simple uses.

Performance: It can be compromised if the app grows.


As a continuation from the first post, we have learnt different possibilities we have to provide our applications with offline functionality. Either with SQLite or with JSON Files, we can get more robust apps that work in low connectivity situations.