Cordova同步永久本地存储

时间:2017-03-13 17:53:12

标签: cordova local-storage

根据我的研究,localStorage对于iOS上的Cordova / Phonegap应用程序是不可靠的,因为当设备内存不足时,它可以被操作系统刷新。

由于我的整个代码是同步编写的,并且我没有重复编写此特定项目的代码,因此我希望直接替换必须同步的localStorage a 同步,这将保留存储的数据而不会出现刷新的威胁。

2 个答案:

答案 0 :(得分:0)

没有同步解决方案。 Cordova存储插件与用于桥接Web和本机功能的底层技术的性质是异步的。 Webview支持的存储选项本质上也是异步的。

您最好的选择是转换为异步存储API。我知道你不想,但是你可以通过使用ES2017的异步/等待功能来加快速度,然后使用TypeScript或Babel转换为ES5。此功能要求您使用的API返回承诺,但那里通常有很多选项。

答案 1 :(得分:0)

I would try to use JSStore. It is not made for Cordova and I'm not completely sure it works, but it is worth the shoot. When you specify the onSuccess parameter, it returns the data, not a promise, as most data persistence API calls do.

I would only recommend it's usage in cases like yours, where a big refactoring is needed to use a form of persistence that uses asynchronous API calls. Even then, you should test the loss of performance of your application.

EDIT: According to the Cordova's storage options page, IndexedDB, persistence mode used in JSStore, only works in Windows, Blackberry 10 and Android (4.4+), so it wouldn't solve your problem. That means that the only way out is refactoring your code to fit the async APIs.