gpt4 book ai didi

dart - 如何在不再需要 Dart future 时删除它

转载 作者:行者123 更新时间:2023-12-03 03:27:40 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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();
}
});
}

关于dart - 如何在不再需要 Dart future 时删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com