Async / waiting for bad practice on Android?

I am currently migrating an existing C # Windows 8 / iOS application to Android (with Xamarin).

I used a lot of asynchronous / pending I / O files, dialogs, networks, etc.

What happens when an application pauses / pauses while waiting? There are two options on Windows and iOS:

  • the application resumes later as if nothing had happened
  • application terminates if memory is low.

In both cases, a memory leak does not occur; there are no changes in the control flow.

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

  • an open dialog will wait forever, which means that objects accessible from the caller ("this", local variables, etc.) will remain in memory forever (memory leak)
  • when the expected network request is completed and the previous activity has already been destroyed by Android, the code after β€œwaiting” (for example, writing a file) may collide because there are two working instances of Activity.

Are my assumptions really? If so, what can be done? (without making the program as complicated as before for inventing async / await)

+6
source share
2 answers

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

How about if you had access to the CancellationTokenSource from your activity. Then in OnPause, you can call Cancel, and then use:

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

See also http://msdn.microsoft.com/en-us/library/jj155759.aspx for canceling async tasks.

More suggestions than the final answer, but I hope this helps.

Edit:

When I started injecting async / await into my code, I found it to look like a zombie virus. As soon as you start ashing, you will find that it spreads throughout the rest of your code. You may have many asynchronous calls for the same reason. There are usually two rules:

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

  • If you are at the top (for example, the user interface) of your code, there may be places where you need to declare the code as async void, because the delegation that you override becomes invalid as the return type. A typical example of this is the button.Click method.
  • I had a database call that was looking for a single value in the database. If I converted this to async, much of my code elsewhere would change. I found that if you are guaranteed, and I mean guaranteed , to be at the bottom of your code, in particular, that none of the methods below the one you call uses async, then you can safely call. The result of the task. This saved me the unnecessary use of my code.

Hope this helps.

+1
source

You can use the service to complete these lengthy tasks. Another option would be to use a mute fragment (one without a view and its .RetainInstance property set to true ).

0
source

All Articles