简体   繁体   English

具有可观察性的离子存储服务

[英]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. 我在Ionic 2应用程序中使用Ionic Storage ,并且在过去几天一直在尝试找到实现服务的最佳方法,该服务使本地存储的数据与组件保持同步。

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. 我没有问题使用Storage as-is(我可以setget正常),但是使服务能够将数据同步到组件,然后回到服务是挑战。

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. 我假设最好的方法是利用rxjs的observables,但我似乎无法找到没有冲突/不一致的文章或者我正在寻找的用例。 Most of them seem to have data only flowing 1 way. 他们中的大多数似乎只有1种方式的数据。 Reference 1 Reference 2 参考文献1 参考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? 什么是保持单个Storage对象(例如user )在服务和组件之间同步而能够修改它的最佳方法,例如在select元素上使用[(ngModel)]

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. 所以我相信你已经找到了自己的anwser。

I think the best way is using singleton shared service or the like. 我认为最好的方法是使用singleton shared service等。 The paradigm of flux , redux and ngrx/store is solution. fluxreduxngrx / store的范例是解决方案。

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. 对于Ionic ,我认为singleton shared servicengrx / store是可用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM