【问题标题】:Completer and Future in dart?飞镖的完成者和未来?
【发布时间】:2012-12-05 21:08:30
【问题描述】:
Future readData() {
    var completer = new Completer();
    print("querying");
    pool.query('select p.id, p.name, p.age, t.name, t.species '
        'from people p '
        'left join pets t on t.owner_id = p.id').then((result) {
      print("got results");
      for (var row in result) {
        if (row[3] == null) {
          print("ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, No Pets");
        } else {
          print("ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, Pet Name: ${row[3]},     Pet Species ${row[4]}");
        }
      }
      completer.complete(null);
    });
    return completer.future;
  }

以上示例代码取自githubSQLJocky Connector

如果可能的话,我希望有人向我解释为什么在 pool.query 之外创建了一个完成器对象的函数然后调用了一个函数 completer.complete(null)。

简而言之,我无法理解打印执行后的部分。

注意:如果可能的话,我还想知道 future 和 Completer 如何用于 DB 和非 DB 操作的实际用途。

我探索了以下链接: Google groups discussion on Future and Completer

和下面给出的 api 参考文档 Completer api referenceFuture api Reference

【问题讨论】:

    标签: dart dart-async


    【解决方案1】:

    从某种意义上说,该方法返回的 Future 对象连接到该完成者对象,该完成者对象将在“将来”的某个时间点完成。在 Completer 上调用 .complete() 方法,它向未来发出它已完成的信号。这是一个更简化的示例:

    Future<String> someFutureResult(){
       final c = new Completer();
       // complete will be called in 3 seconds by the timer.
       new Timer(3000, (_) => c.complete("you should see me second"));
       return c.future;
    }
    
    main(){
       someFutureResult().then((String result) => print('$result'));
       print("you should see me first");
    }
    

    这是link to a blog post which details other scenarios where futures are helpful

    【讨论】:

    • 谢谢,博客文章确实详细解释了。简而言之,我意识到如果我们想要创建相互依赖的事件阶梯,如果我不这样做,它是非常有用的错了。
    • 将“事件阶梯”更改为“异步事件序列”,我认为您的目标是正确的。 :)
    • 代码,即使更新了语法,也不能正常工作。它永远不会打印“you should see me second”。
    • @JohnEvans 博客文章页面链接已损坏。
    • 如果你打算返回一些自定义类型,请将其设置在菱形括号中Completer&lt;MyData&gt; completer = Completer();
    【解决方案2】:

    正确答案在 DartPad 中有错误,原因可能是 Dart 版本。

    error : The argument type 'int' can't be assigned to the parameter type 'Duration'.
    error : The argument type '(dynamic) → void' can't be assigned to the parameter type '() → void'.
    

    下面的sn-p是补码

    import 'dart:async';
    
    Future<dynamic> someFutureResult(){
       final c = new Completer();
       // complete will be called in 3 seconds by the timer.
       new Timer(Duration(seconds: 3), () {     
           print("Yeah, this line is printed after 3 seconds");
           c.complete("you should see me final");       
       });
       return c.future;
    
    }
    
    main(){
       someFutureResult().then((dynamic result) => print('$result'));
       print("you should see me first");
    }
    

    结果

    you should see me first
    Yeah, this line is printed after 3 seconds
    you should see me final
    

    【讨论】:

      【解决方案3】:

      Completer 用于为未来提供一个值,并发出信号以触发附加到未来的任何剩余回调和延续(即在调用站点/在用户代码中)。

      completer.complete(null) 用于向未来发出异步操作已完成的信号。完整的 API 表明它必须提供 1 个参数(即不是可选的)。

      void complete(T value)
      

      此代码对返回值不感兴趣,只是通知调用站点操作已完成。由于它只是打印,您需要检查控制台的输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-16
        • 2019-07-05
        • 2019-03-11
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多