【问题标题】:React Native Realm database: How to return a promise from a write command?React Native Realm 数据库:如何从写入命令返回承诺?
【发布时间】:2017-01-13 07:01:46
【问题描述】:

我在 React Native 应用程序中使用 Realm.io 数据库。我有一个简单的写命令:

    Realm.write(() => {
        Realm.create('Dog', { name: 'Bob'}, true);
    });  

    //Few lines below, make a query for the Bob the dog.

如果由于某种原因写入操作需要超过几毫秒,我如何确保当我查询 Bob the dog 时,我会得到更新的对象?

有没有办法让写操作返回一个承诺或类似的东西,所以我可以确保只有在写操作成功后才能执行代码?

【问题讨论】:

  • 感觉就像是立即按顺序执行操作。不过我不确定。

标签: react-native promise realm


【解决方案1】:

领域同步的。 All callbacks handed to #write are blocking,但这没什么大不了的,因为它针对 React Native 进行了优化。不过,the docs say

写入事务会产生不可忽略的开销 - 您应该构建代码以尽量减少写入事务的数量。

【讨论】:

  • 这种情况下的交易是什么?如果我在write 中调用三个create,这意味着三个事务(三个创建)还是只有一个(一个写入)?
  • 我相信这只是一个。
  • 尽管这个话题已经很老了,但我想添加到@Fermuch 我想说每个write 都算作一个事务,而不是creates,所以write 让你团体交易,这是我的理解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 2020-11-20
  • 2021-04-18
相关资源
最近更新 更多