Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Storage service with observables

I'm using Ionic Storage in an Ionic 2 app and have been trying for the past few days to find the best way to implement a service that keeps the locally stored data in sync with the components.

I have no problem using Storage as-is (I can get set and get just fine), but making the service be able to sync data to the component, and then back to the service is the challenge.

I'm assuming the best way would be to utilize observables with rxjs, but I can't seem to find articles that aren't conflicting/inconsistent or not the use case I'm looking for. Most of them seem to have data only flowing 1 way. Reference 1 Reference 2

What would be the best way to keep a single Storage object (user, for example) in sync between the service and components while being able to modify it, such as using [(ngModel)] on a select element?

like image 368
scferg5 Avatar asked Dec 12 '16 22:12

scferg5


1 Answers

Maybe it is too late to anwser it. Ionic framework have changed a lot. And other useful framework have also come out. So I believe you already found your own anwser.

I think the best way is using singleton shared service or the like. The paradigm of flux, redux and ngrx/store is solution.

What you acheive is handling data in sync. But it is not possible (at least for now).

So possible way is that service (or something) should modify data single-handed, while other components and services have to dispatch event to the service for modifying data. And then, they got identical modified data from the service. After all, sync holds.

For Ionic, I think singleton shared service and ngrx/store is available solution.

like image 122
Hyuck Kang Avatar answered Oct 13 '22 02:10

Hyuck Kang