All we need is an easy explanation of the problem, so here it is.
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?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Depending on the amount of data you wish to cache, you can save it in local storage.
The logic would be like this:
- Make an $http request to your API
- In the
.successstringify the response and store in local storage
- In the
.errorparse the stored local storage response
- Populate the same
$scopevariable from both the
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:
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:
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:
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂