【问题标题】:How to delete a Dart future when it's no longer needed如何在不再需要 Dart 未来时删除它
【发布时间】:2015-02-21 00:30:33
【问题描述】:

这与is there any way to cancel a dart Future?有关

在我的例子中,没有 HTTP,只有昂贵的计算。我有一个滚动浏览的表格/列表。随着元素变得可见,我生成期货来显示计算结果。但是如果我(最终用户)快速滚动,一些结果将“滚动到视野之外”并且不再需要。这可能是一个很大的数字,并且会严重延迟在当前可见元素中显示的有用的期货(结果)的返回:-)。可以做点什么吗?
干杯,史蒂夫

【问题讨论】:

    标签: dart dart-async


    【解决方案1】:

    您可以设置一个标志,指示延迟代码(从期货运行)不再需要结果。 当延迟代码被调用时,它只是返回。

    library cancel_future;
    
    import 'dart:async' show Future, Timer;
    import 'dart:math' show Random;
    
    typedef void TaskFunction(Task task);
    
    // Container for a task
    class Task {
      // an assigned task id
      final id;
      // data to process
      int data;
      // Indicate to the task function, that it should stop processing
      bool isCanceled = false;
      // The task function must set this flat to true when all work is done.
      bool isFinished = false;
      // The task function which processed the data and sets the result.
      TaskFunction fn;
      // The result set by the task function when it finished processing.
      int result;
    
      Task(this.id, this.data, this.fn);
    
      // Start processing the task.
      void execute() => fn(this);
    }
    
    final rnd = new Random();
    
    void main(List<String> args) {
    
      // create tasks
      final tasks = new List<Task>.from(generate());
    
      // start all tasks
      tasks.forEach((t) => t.execute());
    
      // after random delay cancel all unfinished tasks
      new Future.delayed(new Duration(seconds: rnd.nextInt(10)), () {
        tasks.forEach((t) {
          if (!t.isFinished) {
            t.isCanceled = true;
          }
        });
      }).then((_) {
        // check results
        int done = 0;
        int canceled = 0;
        tasks.forEach((t) {
          print(
              'Task id: ${t.id}; isCanceled: ${t.isCanceled}; isFinished: ${t.isFinished}; data: ${t.data}; result: ${t.result}');
          if (t.isFinished) {
            done++;
          }
          if (t.isCanceled) {
            canceled++;
          }
        });
    
        print('Canceled: $canceled.');
        print('Done: $done.');
      });
    }
    
    // geneator for job 100 jobs
    Iterable<Task> generate() sync* {
      int i = 0;
    
      while (i++ < 100) {
        yield new Task(i, rnd.nextInt(100), calc);
      }
    }
    
    // job function
    void calc(Task t) {
      // do a bit of work every 100ms to simulate longer processing
      new Timer.periodic(new Duration(milliseconds: 100), (timer) {
        var result = 0;
        // check if jost was canceled and stop processing in case it was.
        if (t.isCanceled) {
          timer.cancel();
          return;
        }
        // while not finished do a chunk of work
        if (result < t.data) {
          result++;
        } else {
          // finished - clean up and store result
          t.isFinished = true;
          t.result = result;
          timer.cancel();
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-20
      • 2023-01-29
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多