为什么我们不必等待Hive Flutter的未来?
hive 文档说:
您可以像地图一样使用蜂巢。 不必等待期货。
var box = Hive.box('myBox');
box.put('name', 'David');
var name = box.get('name');
print('Name: $name');
我们怎么不必等待?
如果设备运行缓慢,是否没有风险put
和get
命令在打开box
之前运行?
我的理解是,您必须使用等待
,或者冒着难以解决时间问题的风险。
Hive documentation says:
You can use Hive just like a map.
It is not necessary to await Futures.
var box = Hive.box('myBox');
box.put('name', 'David');
var name = box.get('name');
print('Name: $name');
How come we don't have to await?
If the device is running slow, is there no risk that the put
and get
commands will run before the box
was opened?
My understanding was that you had to use await
or risk some hard to troubleshoot timing issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自Hive Docs -
您可能会想知道为什么写作没有异步代码。这是蜂巢的主要优势之一。
更改将在后台尽快写入磁盘,但立即通知所有听众。如果异步操作失败(不应该),则将再次通知所有听众,并以旧值通知。
如果要确保写操作成功,请等待其
Future
。From Hive Docs -
You may wonder why writing works without async code. This is one of the main strengths of Hive.
The changes are written to the disk as soon as possible in the background but all listeners are notified immediately. If the async operation fails (which it should not), all listeners are notified again with the old values.
If you want to make sure that a write operation is successful, just await its
Future
.“官方”基准实用程序将向您表明,写操作的数量级比阅读慢。
例如(在随机Android选项卡上,在发布模式下):读取,字符串,20K记录:192ms与写入,字符串,20K记录:22,509ms。
因此,作为最佳实践,我正在等待写操作,但不要等待阅读。
还记得,即使在非Async操作中,您也要在非常复杂的字符串上执行极其复杂的逻辑 - 这可能需要时间,不是吗?您认为Flutter如何处理这个?没有等待...
The 'official' benchmark utility will show you that write operations are order of magnitude slower than read.
For example (on a random Android tab, in release mode): Read, String, 20K records: 192ms vs Write, String, 20K records: 22,509ms.
So, as a best practice, I await write operations, but do not await read.
Remember that even in non-async operations, say you are performing extremely complex logic on a very large array of Strings - this can take time, can't it? How do you think flutter handles this? There is no await...