Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.ConfigureAwait(false) in Xamarin - safe to use / recommended to use?

Rule of thumb says: if it's not a UI related method, use Task.ConfigureAwait(false).

What if I have a PCL core library which accepts an interface IUIAccess. The view model in the core library then has a method:

public Task ViewModelLoginAsync()
{
  bool success = await loginService.ServiceLoginAsync();
  if(!success)
  {
    uiAccess.ShowInfoDialog("Login failed.");
  }
}

IUIAccess would show a UIAlertView on iOS.

My assumption would now be: if I call ViewModelLoginAsync from my UIViewController I should not configure await to FALSE. It clearly is UI code. However the call to ServiceLoginAsync could use configure await false. Is this correct? Is it just good practice or really recommended because of performance, memory usage, ...?

like image 560
Krumelur Avatar asked Jan 24 '14 19:01

Krumelur


1 Answers

First, I recommend (as much as possible) not to call from the VM into the view. I would prefer to use data binding or a message bus of some kind.

That said, you would not use ConfigureAwait(false) in ViewModelLoginAsync, since it needs to resume on the UI context (this is also true if you update via data binding instead of an explicit call). However ServiceLoginAsync should use ConfigureAwait(false) for the reasons I describe in my MSDN article (primarily performance).

like image 58
Stephen Cleary Avatar answered Sep 20 '22 12:09

Stephen Cleary