ionic framework sync data and work offline

Stack two picture Stack two · Feb 19, 2015 · Viewed 13.2k times · Source

I'm developing an app using ionic framework, and it will fetch some data from an API which was built using laravel, save it in a database so when the user is offline he can continue use the app, I did a little research on the internet and found a plugin called pouchDB, is it recommended for what I need? what is the best way to do it?

Answer

Nic Raboy picture Nic Raboy · Feb 20, 2015

Depending on the amount of data you wish to cache, you can save it in local storage.

The logic would be like this:

  1. Make an $http request to your API
  2. In the .success stringify the response and store in local storage
  3. In the .error parse the stored local storage response
  4. Populate the same $scope variable from both the .success and .error

This way if your requests succeed (the device has internet and the API is functional) the data is stored. Otherwise, the requests fail (no internet or other reason), use the stored data.

An example of this can be seen here:

https://www.thepolyglotdeveloper.com/2014/06/saving-data-with-ionicframework/

However, like mentioned in a previous answer, PouchDB is a great option if you have a CouchDB database setup. Here are a few Ionic tutorials for syncing solutions:

https://www.thepolyglotdeveloper.com/2014/12/syncing-data-firebase-using-ionic-framework/ http://devgirl.org/2014/12/30/sync-data-using-pouchdb-in-your-ionic-framework-app/

Regards,