ionic framework sync data and work offline
Asked Answered
D

2

8

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?

Dogger answered 19/2, 2015 at 15:33 Comment(1)
can we do this without pouchDb, i mean how about with mysqlGametocyte
B
5

PouchDb is sure a good solution to provide data synchronization to your app. But depending from what you need to do you could choose between several PaaS that provide natively data synchronization and offline capabilities(along with many other features like oAuth2 authentication and push notification).

I have worked with a few i can suggest:

  1. Parse
  2. Firebase
  3. Microsoft Azure mobile services

All these platforms provide clients to work with for all native mobile environments and javascript.

Behemoth answered 19/2, 2015 at 15:56 Comment(11)
Thank you for your help Riccardo, one question, can PouchDb sync the data from any API or it only works with CouchDb? Because my API was built using laravel, I'm pretty new to ionic, I appreciate your patienceDogger
Hi @Stacktwo, your backend must support the couchDB replication protocol. This issue is not related to your API or ionic but to your dbms(maybe you use MySQL?)Behemoth
@Stacktwo i suggest you to sync your data to a couchDB instance periodically and work with it.Behemoth
I've had good success with a proof-of-concept using pouchDB and IBM Cloudant (I work for IBM).Eastern
@SHC Can I use pouchDB in ionic app & mysql at server & sync them?Phenacetin
PouchDB syncs with couchdb or equivalent environments. It will not sync to mysql.Eastern
@SHC are you sure? Can you provide me some link for the same?Phenacetin
Actually My Admin panel is ready using MySQL. Now i need to create the ionic app. I need to sync data between app & server online offline both. I was thinking of using pouchDB but as you said I can't sync pouchDB & MySQL then what can be the alternative? How I can sync data?Phenacetin
@SHC please let me know if we can chat?Phenacetin
@nGy sure - visit chat.stackoverflow.com/rooms/124974/pouchdb-syncEastern
The problem with Parse is that the JS SDK doesn't support offline capabilities yet... :-/Iata
I
13

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,

Iow answered 20/2, 2015 at 16:50 Comment(3)
Great tutorials! I recently wrote a PouchDB+Ionic tutorial myself: github.com/nolanlawson/pouchdb-ionic#readmeWeichsel
Thanks for the compliment!Iow
what if you have thousands of records, will storing data in local storage would be a good option for working offline ?????? A humble questionGametocyte
B
5

PouchDb is sure a good solution to provide data synchronization to your app. But depending from what you need to do you could choose between several PaaS that provide natively data synchronization and offline capabilities(along with many other features like oAuth2 authentication and push notification).

I have worked with a few i can suggest:

  1. Parse
  2. Firebase
  3. Microsoft Azure mobile services

All these platforms provide clients to work with for all native mobile environments and javascript.

Behemoth answered 19/2, 2015 at 15:56 Comment(11)
Thank you for your help Riccardo, one question, can PouchDb sync the data from any API or it only works with CouchDb? Because my API was built using laravel, I'm pretty new to ionic, I appreciate your patienceDogger
Hi @Stacktwo, your backend must support the couchDB replication protocol. This issue is not related to your API or ionic but to your dbms(maybe you use MySQL?)Behemoth
@Stacktwo i suggest you to sync your data to a couchDB instance periodically and work with it.Behemoth
I've had good success with a proof-of-concept using pouchDB and IBM Cloudant (I work for IBM).Eastern
@SHC Can I use pouchDB in ionic app & mysql at server & sync them?Phenacetin
PouchDB syncs with couchdb or equivalent environments. It will not sync to mysql.Eastern
@SHC are you sure? Can you provide me some link for the same?Phenacetin
Actually My Admin panel is ready using MySQL. Now i need to create the ionic app. I need to sync data between app & server online offline both. I was thinking of using pouchDB but as you said I can't sync pouchDB & MySQL then what can be the alternative? How I can sync data?Phenacetin
@SHC please let me know if we can chat?Phenacetin
@nGy sure - visit chat.stackoverflow.com/rooms/124974/pouchdb-syncEastern
The problem with Parse is that the JS SDK doesn't support offline capabilities yet... :-/Iata

© 2022 - 2024 — McMap. All rights reserved.