brianchance / MvvmCross-UserInteraction

MvvmCross plugin for interacting with the user from a view model
MIT License
43 stars 44 forks source link

AlertView not showing up from async method... #2

Closed mohibsheth closed 11 years ago

mohibsheth commented 11 years ago

Hi,

This may not actually be an issue, but I didnt know how else to contact you so creating this issue so maybe you could help.

I have an MvvmCross app where I load data from a web service. so the command method somewhat looks like this...

public void DoSearch()
{
    var res = await SearchService.GetList();
}

The GetList implementation is...

public async Task<T> GetList()
{
     if(GetNetworkStatus())
          DoNetwork();
     else
          DoLocal();
}

The GetNetworkStatus function is where I am trying to check for network availability and prompt user to retry or go offline.

var retry = await Mvx.Resolve<IUserInteraction>().ConfirmAsync("Network is unavailable. Do you want to Retry or go Offline?", "Network Error", "Retry", "Offline");

The Alert is never shown when I use this line in GetNetworkStatus. If I use this same line in my ViewModel command or even in GetList of my service... it works but does not work in GetNetworkStatus.

Would you happen to know why?

brianchance commented 11 years ago

I think you need to await the GetNetworkStatus call in GetList and make GetNetworkStatus async...

if (await GetNetworkStatus())
mohibsheth commented 11 years ago

Thanks for your prompt response.

I am sorry for not specifying that in my message earlier. I am doing GetNetworkStatus().Result. Does that not await automatically? I will still try as you mentioned tomorrow at work and get back to you.

brianchance commented 11 years ago

Doing .Result should await, however the alert dialog is being run on the UI thread which might be causing confusion.

mohibsheth commented 11 years ago

So await should resolve this issue right? I will certainly try this tomorrow and let you know the outcome.

mohibsheth commented 11 years ago

Hey Brian, Thanks. Using await has resolved the issue. :)