异步/等待Android的下不好的做法? [英] async/await bad practice under Android?

查看:182
本文介绍了异步/等待Android的下不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我移植现有的C#Windows 8 / iOS应用程序到Android(与Xamarin)。

Currently I am porting an existing C# Windows 8 / iOS app to Android (with Xamarin).

我用了很多异步的/指日可待文件IO,对话,网络等...

I used a lot of async/await for file IO, dialogs, network, etc…

这是计谋通话过程中,当应用程序被暂停/停止,会发生什么? 在Windows和iOS有两种可能:

What happens when the app is paused/suspended during an await call? Under Windows and iOS there are two possibilities:

  • 应用程序稍后重新开始,好像什么也没有发生过
  • 如果内存不足
  • 在该应用程序将被终止。

在这两种情况下,没有memoy泄漏,有在控制流中没有改变。

In both cases, there is no memoy leak, there are no changes in control flow.

然而,Android的下一个活动可以被摧毁和重建,而这个过程保持活着。在我的异步的理解/等待这意味着:

However, under Android, an Activity can be destroyed and recreated while the process stays alive. In my understanding of async/await this means:

  • 在一个未关闭的对话框将一直等待,这意味着它是从主叫(本,局部变量等)访问的对象将留在记忆中永远(内存泄漏)
  • 当期待已久的网络请求完成,而前者的活动已经被破坏了Android上,code后,伺机(例如文件写入)可能会发生冲突,因为活动两个正在运行的实例存在。

是我assumtions真的吗?如果是的话,可以做些什么? (未做程序一样复杂异步的本发明的前/等待)

Are my assumtions true? If yes, what can be done? (without making the program as complicated as before of the invention of async/await)

推荐答案

这是Android的活动是保证调用将OnPause前的活动是失活/破坏,OnResume启动时(见的 http://developer.android.com/training/basics/activity-lifecycle/index.html )。

An Android Activity is guaranteed to call OnPause before the activity is deactivated/destroyed and OnResume when it starts up (see http://developer.android.com/training/basics/activity-lifecycle/index.html).

怎么样,如果你有资料可从活动一CancellationTokenSource。然后,在将OnPause你可以打电话取消,然后使用方法:

How about if you had a CancellationTokenSource available from your Activity. Then in OnPause you can call Cancel and then use:

try
{
    // Your async code
    ...
}
catch (OperationCancelledException e)
{
}

参见 http://msdn.microsoft.com/en-us/库/ jj155759.aspx 取消异步任务。

更多的建议不是一个确切的答案,但我希望它能帮助。

More a suggestion than a definitive answer, but I hope it helps.

编辑:

当我开始引入异步/等待进入我的code,我发现它像一个僵尸病毒。一旦你开始asyncing你会发现它那贯穿你的code,其余$ P $垫。这可能是因为你有很多出于同样的原因异步调用。通常有两种规律可循:

When I started introducing async/await into my code I found it to be like a zombie virus. Once you start asyncing you find it spreads throughout the rest of your code. It may be that you've got lots of async calls for the same reason. There are generally two rules to follow:

  1. 在声明的公共方法为异步任务美孚()而不是公共异步无效美孚()
  2. 不要在异步$阻止C $ç
  1. Declare methods as public async Task Foo() instead of public async void Foo()
  2. Don't block on async code

在我自己的实践,我发现2地方,你可以打破这些一般规则。

In my own practice, I've discovered 2 places where you can break these general rules.

  1. 如果你在你的code中的'顶'(即用户界面),有可能是,你必须要申报code作为异步空白的地方,因为你是压倒一切的委托取空的返回类型。这方面的一个典型的例子是用一个button.Click方法
  2. 在我有这抬头单个值在数据库中的数据库调用。如果我转换,要异步,的许多的我的code其他地方将不得不改变。我发现,如果你保证,我的意思是保障,是在你的code中的'底部',特别是没有下面一个你所呼叫使用异步方法,那么你可以安全地调用。结果上的任务。这救了我的asyncing一半的code不必要的。
  1. If you're at the 'top' (i.e. the UI) of your code, there may be places where you have to declare code as async void because the delegate you're overriding takes void as a return type. A typical example of this is with a button.Click method.
  2. I had a database call which looked up a single value in a database. If I converted that to async, lots of my code elsewhere would have to change. I discovered that if you're guaranteed, and I mean guaranteed, to be at the 'bottom' of your code, specifically that none of the methods below the one you're calling use async, then you can safely call .Result on a Task. This saved me from asyncing half my code unnecessarily.

希望这有助于。

这篇关于异步/等待Android的下不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆