IndexedDB 在 Chrome 刷新到磁盘

IndexedDB flush to disk on Chrome

我在 Chrome 上遇到 IndexedDB 问题,一旦事务 returns 成功写入,我就会重新加载我的页面。

问题是有时数据在重新加载后不反映。我可以通过在重新加载前给出大约 100 毫秒的超时来解决这个问题,这让我相信数据不会每次都刷新到磁盘。

Firexox 有一个 experimental readwriteflush mode 可确保在返回成功调用之前将数据刷新到磁盘,但似乎无法为 Chrome 找到类似的。有什么建议吗?

这是我的插入代码:

const data = {type: type, value: value};

const objectStore = StorageService.db.transaction(['localData'], 'readwrite').objectStore('localData');

// readwriteflush doesn't work in chrome
// const objectStore = StorageService.db.transaction(['localData'], 'readwriteflush').objectStore('localData');

const requestSet = objectStore.put(data);

requestSet.onerror = function (event) {
    alert('Error in saving data locally');
};

requestSet.onsuccess = function (event) {
    console.log('Data was successfully saved locally: ' + type);
    if (callback != undefined) {
        callback();
    }
};

回调在其中执行了 location.reload = '/';(以及其他一些事情),因此页面会在返回 onsuccess 后重新加载。

页面重新加载后,我无法通过代码和开发人员工具在我的 IndexedDB 存储中看到任何数据。然而,这并不总是发生,我观察到只有当数据比平常大时才会发生这种情况。

"success" 在请求中触发并不表示事务已成功提交。由于单独的失败请求(例如冲突的添加调用)、I/O 错误或例如断电。

您需要等待 "complete" 事件在交易中触发。 Chrome 在触发 "complete" 事件之前刷新到磁盘。