Restful API with local SQLite database

Hi there,
Need some advice from the Ionic 2 experts please, any pointers or advice would be appreciated!

We are building a mobile app that will be fetching data from a restful API. This data is then stored into a local SQLite database and the app should always be loading from the local DB and the DB then synchronised / updated via the API.

The idea is to have a service that provides the data and should be available all throughout the app and ideally immediately fetch and return the data from the DB. It should however also check for newer data on the server and update this data in the DB. This data update should also immediately then update on the interface.

What is the best approach for creating the service? And more specifically:

  • At what point should should we sync the DB with the API and where would the best place be to start this sync operation? Should it be the app component constructor?
  • How do we get the service to load the DB data and have the result of the sync then update the data, assuming that the interface is at this point already displaying the data from the DB?

Thanks in advance for any useful insight you might have into this.

I have tried creating a service, which now contains a Sync method which connects to the API and updates the SQLite database. We also then have a get function which returns the data that is in the DB.

The view can now successfully call and get the data via the get method but this data is never updated as a result of the sync until the app reloads again.

Where should we be calling this sync method and also ensure that the view always has access to the latest data?

Can u please post the code which u have implemented.