当我尝试从数组作为关键路径删除IndexedDb中的记录时,我有一个有趣的案例:
remove(storeName: string, key: any) { // key = ['uuid1', 'uuid2']
const open$ = this.open(this.schema.name, this.schema.version);
return mergeMap.call(open$, (db: IDBDatabase) => Observable.create((transactionObserver) => {
const transaction = db.transaction([storeName], IDB_TRANSACTION_TYPE.READWRITE);
const objectStore = transaction.objectStore(storeName);
const getRequest = objectStore.delete(key);
const onTransactionError = (err) => { transactionObserver.error(err); };
const onTransactionComplete = () => {};
const onRecordDelete = (event: any) => { // eslint-disable-line
transactionObserver.next();
transactionObserver.complete();
};
transaction.addEventListener(IDB_EVENT.COMPLETE, onTransactionComplete);
transaction.addEventListener(IDB_EVENT.ERROR, onTransactionError);
getRequest.addEventListener(IDB_EVENT.SUCCESS, onRecordDelete);
return () => {
getRequest.removeEventListener(IDB_EVENT.SUCCESS, onRecordDelete);
getRequest.removeEventListener(IDB_EVENT.ERROR, onTransactionError);
transaction.removeEventListener(IDB_EVENT.COMPLETE, onTransactionComplete);
};
}));
}
删除方法和事务不返回任何错误,状态为“完成”,但storeObject值仍然存在。 谢谢你的帮助!