如何使indexeddb事务保持活动状态? [英] How do you keep an indexeddb transaction alive?

查看:118
本文介绍了如何使indexeddb事务保持活动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要您使用适当的IDBTransaction,就可以通过单个事务来完成所有事务(而不是打开多个事务(读取表,写入表,写入另一个表等)?

Mozilla说:使事务保持活动状态的唯一方法是在其上发出请求.完成请求后,您将收到一个DOM事件,并假设请求成功,您将有另一个机会在该回调期间扩展事务."这有点模糊.这是否意味着如果我为DOM回调提供了一个事件处理程序,那么我可以在该回调中的任何时候使用事务,而不必担心事务会被关闭?

https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB#Adding_data_to_the_database

解决方案

IndexedDB事务在触发最后一个回调后立即提交,因此保持它们存活的方法是通过回调传递它们.

我正在从Jonas Sicking采购我的交易信息,Jonas Sicking是IndexedDB的Mozilla开发人员和共同规范作家,他评论了

当变量用完时,事务永远不会自动提交 范围.通常,它们仅在上一次成功/错误回调时才提交 触发,并且该回调不调度更多请求.所以不是 与任何变量的范围有关.

唯一的例外是如果您创建交易但未放置 反对它的要求.在这种情况下,交易是已提交"的 (这意味着对于没有请求的交易) 当您返回事件循环时.在这种情况下,您可以 从技术上讲,一旦对交易的所有引用都提交"该交易 超出范围,但这并不是一个特别有趣的用例 优化.

Instead of opening several transactions (read a table, write to a table, write to another table, etc) is it possible to do this all from a single transaction as long as you are using an appropriate IDBTransaction?

Mozilla says: "The only way to keep the transaction active is to make a request on it. When the request is finished you'll get a DOM event and, assuming that the request succeeded, you'll have another opportunity to extend the transaction during that callback." which is a little vague. Does that mean if I provide an event handler for the DOM callback that I can use the transaction at any point in that callback without ever having to worry about the transaction being closed?

https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB#Adding_data_to_the_database

解决方案

IndexedDB transactions commit as soon as the last callback is fired, so the way to keep them alive is to pass them along via callbacks.

I'm sourcing my transaction info from Jonas Sicking, a Mozilla dev and co-spec writer for IndexedDB, who commented on this excellent blog post to say the following:

The following sentence isn't correct "Transactions today auto-commit when the transaction variable goes out of scope and no more requests can be placed against it".

Transaction never automatically commit when a variable goes out of scope. Generally they only commit when the last success/error callback fires and that callback schedules no more requests. So it's not related to the scope of any variables.

The only exception to this is if you create a transaction but place no requests against it. In that case the transaction is "committed" (whatever that means for a transaction which has no requests) as soon as you return to the event loop. In this scenario you could technically "commit" the transaction as soon as all references to it go out of scope, but it's not a particularly interesting use case to optimize.

这篇关于如何使indexeddb事务保持活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆