为什么我们不必等待Hive Flutter的未来?

发布于 2025-02-06 09:00:13 字数 457 浏览 2 评论 0原文

hive 文档说:

您可以像地图一样使用蜂巢。 不必等待期货。

var box = Hive.box('myBox');
box.put('name', 'David');
var name = box.get('name');
print('Name: $name');

我们怎么不必等待?

如果设备运行缓慢,是否没有风险putget命令在打开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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2025-02-13 09:00:13

来自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.

土豪我们做朋友吧 2025-02-13 09:00:13

“官方”基准实用程序将向您表明,写操作的数量级比阅读慢。

例如(在随机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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文