如何解决使用Dexie的本地存储不会保持持久
我正在使用Dexie来访问闪存卡制造商项目上的IndexedDB。我可以按预期操作数据库,如果我关闭并重新打开浏览器,它将保留在数据库中,但是通常,当我几天不工作后打开该项目时,存储在数据库中的数据就是不再在那里。
我做了一些研究,发现我需要使数据库持久化,但是我不知道该怎么做。
我已经将推荐的代码从Dexie Storage Manager page复制到主页index.js的onLoad函数中。这是相关的代码:
//When the page loads,display the decks in ul on the webpage.
window.addEventListener('load',onLoad);
async function onLoad() {
//Check how much data is available.
showEstimatedQuota();
//Make sure the storage is persistent.
isstoragePersisted().then(async isPersisted => {
if (isPersisted) {
console.log(":) Storage is successfully persisted.");
} else {
console.log(":( Storage is not persisted.");
console.log("Trying to persist..:");
if (await persist()) {
console.log(":) We successfully turned the storage to be persisted.");
} else {
console.log(":( Failed to make storage persisted");
}
}
});
}
上面的onLoad函数引用了我保存在dexie-setup.js上的三个函数:
//This function makes the storage persistent.
//(copied from https://dexie.org/docs/StorageManager)
async function persist() {
return await navigator.storage && navigator.storage.persist &&
navigator.storage.persist();
}
//This function checks if the storage is persistent.
//(copied from https://dexie.org/docs/StorageManager)
async function isstoragePersisted() {
return await navigator.storage && navigator.storage.persisted &&
navigator.storage.persisted();
}
//This function logs to console how much data is available.
//(copied from https://dexie.org/docs/StorageManager)
async function showEstimatedQuota() {
if (navigator.storage && navigator.storage.estimate) {
const estimation = await navigator.storage.estimate();
console.log(`Quota: ${estimation.quota}`);
console.log(`Usage: ${estimation.usage}`);
} else {
console.error("StorageManager not found");
}
}
我的控制台日志:
-
dexie-setup.js:56配额:6358499328
-
dexie-setup.js:57用法:25370
-
index.js:30 :(存储不持久。
-
index.js:31尝试 坚持..:
-
dexie-setup.js:84完成了对dexie的检查。
-
index.js:33 :)我们成功地使存储得以保留。
但是,如果刷新页面,则会在控制台上记录相同的内容:数据库仍设置为非持久性。
showEstimatedQuota函数检查数据存储并确认DataStorage API正常运行,所以我认为这不是问题。 (我只在其中存储带有文本的小对象,因此无论如何我都不希望超出存储限制。)
到目前为止,该项目完全在我的chromebook上本地化,我正在Chrome浏览器上查看它。
请让我知道如何使数据库持久化。我对此很陌生(这是我关于stackoverflow的第一个问题!),因此希望这是一个容易解决的问题!预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。