Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Thread issue with view model in MVVMCross

Tags:

mvvm

mvvmcross

I am using MVVMCross with my cross-platform Windows Phone and Android app. In the core project's main view model, I am doing some background work using TPL and I want to make sure that in the callback, when I make changes to the properties of the view model which will trigger UI change, that the code is run on UI thread, how do I achieve this?

For code, here is how it likes

    private MvxGeoLocation _currentLocation;
    private Task<MvxGeoLocation> GetCurrentLocation()
    {
        return Task.Factory.StartNew(() =>
            {
                while (_currentLocation == null && !LocationRetrievalFailed)
                {
                }
                return _currentLocation;
            });
    }

    var location = await GetCurrentLocation();
    if (LocationRetrievalFailed)
    {
        if (location == null)
        {
            ReverseGeocodingRequestFailed = true;
            return;
        }
        // Show toast saying that we are using the last known location
    }
    Address = await GooglePlaceApiClient.ReverseGeocoding(location);
like image 386
imgen Avatar asked Feb 12 '14 13:02

imgen


1 Answers

Did you try IMvxMainThreadDispatcher?

var dispatcher = Mvx.Resolve<IMvxMainThreadDispatcher>();
dispatcher.RequestMainThreadAction(()=> { .... });

See more on the implementation:

https://github.com/MvvmCross/MvvmCross/search?q=IMvxMainThreadDispatcher&type=Code

Usually I don't think you need this though.

Since you start the async processing from main thread, the async operations should return back to main thread.

Can you give an example of the async code you are doing?

like image 81
WriteEatSleepRepeat Avatar answered Sep 28 '22 10:09

WriteEatSleepRepeat