如何取消当前的 `await .then((value){….}`。

huangapple go评论78阅读模式
英文:

How could i cancel any current await .then((value){....}

问题

我正在将Firestore用作我的应用程序的后端。

在我的应用程序中,用户可以进行注册,然后使用Firabese auth登录,就像任何其他应用程序一样。此外,如果他们有多个帐户,他们还可以切换到其他帐户。

在我的应用程序中,我有很多.get().then((value){....},如下所示:

FirebaseFirestore.instance.collection('users')
.limit(20)
.get(const GetOptions(source: Source.server)).then((value){
   // 这里我使用一些任务来初始化一些全局变量,就像下面这样
   // myGlobalVariable = value.get('name');       
});

Firestore非常强大且快速,它可能需要一秒钟才能执行完.get()函数内部的操作。

但在经过数月的测试后,我注意到有时需要5-8秒才能完成.get(),当然这是因为用户端的一些慢速连接或其他原因。老实说,这种情况很少发生。但这让我担心的原因如下:

  1. 用户使用"帐户1"登录。

  2. 用户有一些.get().then((value){myGlobalVariable = value.get('name');}方法(由于某种原因发生了延迟)。

  3. .get().then((value){myGlobalVariable = value.get('name');}完成之前,用户切换到"帐户2"。

  4. 在用户成功切换到"帐户2"后,.get().then((value){myGlobalVariable = value.get('name');}成功完成。

这里发生了一些灾难,因为myGlobalVariable具有来自第一个旧帐户的旧值。

实际上,这只是一个简单的例子。我有很多全局变量以及.then((value){....}内部的一些任务,每个.then((value){....}仅依赖于当前帐户。

我该如何确保在用户切换到另一个帐户之前取消整个当前未完成的.then((value){....},或者至少有什么最好的方法来避免这种情况发生?

英文:

i am using Firestore as backend to my app

in my app users could signUp then login in using Firabese authlike any app . also they can switch their accounts to another if they have many accounts.

in my app i have many .get().then((value){....} like following ..

FirebaseFirestore.instance.collection('users')
.limit(20)
.get(const GetOptions(source: Source.server)).then((value){
   // here i am using some tasks to init some global variables like following
   // myGlobalVariable = value.get('name');       
});

Firstore is awesome and fast and it take maybe a second to after .get() function and run what inside .then((value){....}

but after many tests in months work. i noticed sometimes there is 5-8 sec to finish from .get() and this of course because some slow connection of the users side or any reason else . To be honest, this rarely happens . But this keeps worrying me because of the following

1- user login in using account 1

2- user have some .get().then((value){myGlobalVariable = value.get('name');} methods (Some delay happened for some reason)

3- before .get().then((value){myGlobalVariable = value.get('name');} is done, user switch his account toaccount 2

4- .get().then((value){myGlobalVariable = value.get('name');} successfully done after user successfully done log in account 2

here is some disaster happened . because myGlobalVariable has old value from first old account .

in the fact it is just simple example . i have lots of global variables and some tasks within .then((value){....} that per .then((value){....} depends only in current account

How could i make sure and cancel the whole current pending `.then((value){....} before user switch to another account .

or at least what is the best way to avoid this !

答案1

得分: 0

  1. 将当前用户的 UID 传递给启动从Firestore读取用户数据的函数。
  2. 一旦then回调触发,添加一个检查,确保当前的 UID 仍然与调用时相同。
英文:

You could:

  1. Pass the current user's UID to the function that starts reading the user's data from Firestore.
  2. Once then then callback fires add a check that the current UID is still the same as what you were called with.

huangapple
  • 本文由 发表于 2023年3月3日 22:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定