gpt4 book ai didi

javascript - 将 localForage 与 Cordova 结合使用的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-01 02:50:27 25 4
gpt4 key购买 nike

我正在尝试使用localForagelocalForage-cordovaSQLiteDriver在 cordova 应用程序中设置持久存储。它不起作用,我不断将以下条目记录到控制台:

new transaction is waiting for open operation

我不确定我是否正确理解 localforage 生命周期。这就是我所拥有的:

// 1) setup cordova SQL lite driver
localforage.defineDriver(window.cordovaSQLiteDriver).then(function() {

// 2) set preferred driver order
return localforage.setDriver([
window.cordovaSQLiteDriver._driver, // <-- prefer cordovaSQLiteDriver
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
]);
})
.then(function() {
// 3) wait for localForage to be ready
return localforage.ready();
})
.then(function() {

// 4) create a globally scoped app database
window.appStorage = localforage.createInstance({
version: 1.0,
size: 52428800, // 50mb
name: 'my-app-name',
storeName: 'setup'
});

// 5) check if we're using cordova sql lite
if (localforage.driver() !== window.cordovaSQLiteDriver._driver) {
console.warn('Not using cordovaSQLiteDriver.');
}

// 6) write something
return appStorage.setItem('message', 'It worked!'); // <-- only it doesn't
})
.then(function() {
// 7) get the value back out of the database
return appStorage.getItem('message');
})
.then(function(message){
// 8) write the value to the console
console.log(message);
});

在我看来,它应该将它成功了!打印到控制台。

最佳答案

因此,首先,createInstance() 返回一个全新且不相关的 localforage 实例,并从头开始重新初始化。因此,新实例将尝试使用默认驱动程序首选项顺序(IndexedDB、WebSQL、LocalStorage)来决定使用哪个驱动程序。所有 localforage 实例共享的唯一内容是可用/定义的驱动程序。所以你可以做这样的事情:

localforage.defineDriver(window.cordovaSQLiteDriver).then(function() {
window.appStorage = localforage.createInstance({
version: 1.0,
size: 52428800, // 50mb
name: 'my-app-name',
storeName: 'setup',
driver: [
window.cordovaSQLiteDriver._driver, // <-- prefer cordovaSQLiteDriver
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
]
// OR instead of passing the `driver` option,
// you can call `window.appStorage.setDriver()`
// right after `createInstance()`
});

if (window.appStorage.driver() !== window.cordovaSQLiteDriver._driver) {
console.warn('Not using cordovaSQLiteDriver before setItem.');
}

return appStorage.setItem('message', 'It worked!');
})
.then(function() {
return appStorage.getItem('message');
})
.then(function(message){
console.log(message);
if (window.appStorage.driver() !== window.cordovaSQLiteDriver._driver) {
console.warn('Not using cordovaSQLiteDriver after setItem.');
}
});

关于javascript - 将 localForage 与 Cordova 结合使用的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47005022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com