到底什么时候使用async-await然后呢?

到底什么时候使用async-await然后呢?

问题描述:

对此我感到非常困惑。我要求您澄清一下概念。

I am very confused about this. I request you to clarify the concept.

请考虑以下情形:

案例1:

int number = 0; 

void calculate() {
   number = number + 2;
   print(number);
}

我知道这很好。 2将显示在终端上。

I know this works just fine. "2" will be printed on the terminal.

但是我为什么不应该使用 async - await 在这里,像这样:

But why shouldn't I use async-await here, like this:

 int number = 0; 

void calculate() async {

 void addition() async {
   number = number + 2;
  }

 await addition();
   print(number);
}

在我看来这很合逻辑,因为 print(number )应该等待 number = number + 2 完成。为什么这没有必要? dart如何知道要先执行哪个操作?

This seems logical to me, since print(number) should wait for number = number + 2 to finish. Why isn't this necessary? How does dart know which operation to execute first?

如何确保在 number = =之前不执行 print(number)数字+ 2 并在终端上打印 0?

How is it ensured that print(number) isn't executed before number = number + 2 and "0" is printed on the terminal?

Does the sequence in which we write these operations in the function matter?

案例2:
考虑我正在互动的情况与 SQFLite 数据库和获取的值相互依赖。

Case 2: Consider the case where I am interacting with SQFLite database and values fetched depend on each other.

注意: number1 number2 number3 在以下 function 被调用。

Note: number1, number2, number3 will still have values before the following function is called.

void getValues() async {

  void calculate1() {
   number1 = await db.getNumber1(10);
  }

  void calculate2() {
   number2 = await db.getNumber2(number1);
  }

  await calculate1().then((_) async {
    await calculate2().then((_) async {
     number3 = await db.getNumber3(number2);
    });
  });

}

我的应用程序中有很多这类功能我到处都在做

I have a lot of these types of functions in my app and I am doing this everywhere.

我有点偏执,想知道 number1 number2的旧值 getNumber2() getNumber3()中作为参数 ,那么我就注定了。

I am kind of paranoid, thinking if old values of number1and number2 are taken as a parameter in getNumber2() and getNumber3() respectively, then I'll be doomed.

您可以看到它就像一个主要的线程通常是UI线程。您在该线程中编写的所有操作都将逐行执行,并且在完全执行一行之后将移至下一行。

You can see it like there is one main thread in general which is the UI thread. Any operations you are writing in this thread will be performed line by line and after completely executing one line it will move to next line.

现在假设您知道一些东西,要知道要计算还是完全执行结果或错误,都需要花费一些时间。如果将其写在主UI线程(同步线程)中,则意味着要停止应用程序的UI,这又会使应用程序崩溃(应用程序无响应错误),因为操作系统认为应用程序已冻结但是您知道这是由于计算而发生的,因为您正在UI线程中运行,这很花时间,UI正在等待其完全执行。

Now suppose you have something which you know that it will take time to be computed or fully executed with either a result or error. If you will write this in the main UI thread (synchronous thread) that means you're stopping the UI of the app, which in turn makes the app to crash(Application Not Responding Error) as the operating system feels that the app has frozen but as you know this is happening because of the compute you are running in the UI thread which is taking time and the UI is waiting for it to be completely executed.

所以为了克服这个问题,我们使用异步方法来计算时间,例如从数据库中获取一些数据,这些数据将返回未来的值或错误。主UI线程不等待异步线程。如果您在异步任务完成之前没有任何显示给用户,则可以暂时放置加载指示器。

So to overcome this issue we use Asynchronous methods to compute the time taking computations like getting some data from a database which will return a value or error in "future". The main UI thread doesn't waits for the asynchronous threads. If you don't have anything to show to the user until any asynchronous task is completed you place the loading indicators for the time being.

希望这会有所帮助!