Xamarin Diaries – Offline by default (Part II)

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 https://www.nuget.org/packages/SQLitePCLRaw.bundle_sqlcipher/ 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:

public class IncidentType
    public string Id { get; set; }
    public Description Description { get; set; }

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: https://github.com/dhompanera/XamarinDiaries/tree/master/Offline

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 (https://www.newtonsoft.com/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.

Related Articles

Xamarin Diaries – Offline by default

Xamarin Diaries – Stepped Slider

Xamarin Diaries – Working with Maps

About Daniel Hompanera

I'm SW Architect and Team Leader at Solid Gear. Passionated about technology and methodology, what actually makes me happy is to work with People day by day and everything that can make things get better. I don't have favourite platform or technology, although I've been working with .NET technologies for all these almost 10 years and I feel an special appeal to it. I've worked Agile, not so Agile and Fragile environments but if I could choose, of course I would choose Solid Gear's Agile Method with no doubts

2 thoughts on “Xamarin Diaries – Offline by default (Part II)”

  1. You should look at using Akavache for storing data on the mobile device – there are options for in-memory or on the device in different locations.

    • Yes, thanks for the reference. Akavache is a good option too but in my opinion, specially with training purposes, I prefer to keep as much control as possible on what is going on with cached data. Anyway, this is a good recommendation for Part III 😉

Leave a Comment

Responsable » Solidgear.
Finalidad » Gestionar los comentarios.
Legitimación » Tu consentimiento.
Destinatarios » Los datos que me facilitas estarán ubicados en los servidores SolidgearGroup dentro de la UE.
Derechos » Podrás ejercer tus derechos, entre otros, a acceder, rectificar, limitar y suprimir tus datos.

¿Necesitas una estimación?

Calcula ahora