[PWA] 16. Clean IDB
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[PWA] 16. Clean IDB相关的知识,希望对你有一定的参考价值。
When we save items to IndexDB, we need to think about clean the items or keep those in a short list.
IndexController.prototype._onSocketMessage = function(data) { var messages = JSON.parse(data); this._dbPromise.then(function(db) { if (!db) return; var tx = db.transaction(‘wittrs‘, ‘readwrite‘); var store = tx.objectStore(‘wittrs‘); messages.forEach(function(message) { store.put(message); }); // TODO: keep the newest 30 entries in ‘wittrs‘, // but delete the rest. // // Hint: you can use .openCursor(null, ‘prev‘) to // open a cursor that goes through an index/store // backwards. store.index(‘by-date‘).openCursor(null, ‘prev‘) .then((cursor)=>{ return cursor.advance(30); // only 30 itmes are kept }).then(function deleteCursor(cursor){ if(!cursor){ return; }else{ cursor.delete(); return cursor.continue().then(deleteCursor); } }) }); this._postsView.addPosts(messages); };
以上是关于[PWA] 16. Clean IDB的主要内容,如果未能解决你的问题,请参考以下文章
[PWA] 18. Clean the photo cache
[PWA] 13. New db and object store
[io PWA] keynote: Launching a Progressive Web App on Google.com