Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide examples of using different subscription methods in MvvmCross


Can you provide examples of using different subscription methods in MvvmCross


MvvmCross provides a powerful messaging system through its `IMvxMessenger` interface, allowing components to communicate with each other by publishing and subscribing to messages. Here are examples of using different subscription methods in MvvmCross:

Basic Subscription

The most straightforward way to subscribe to messages is by using the `Subscribe` method. This method receives messages synchronously on the same thread where the message was published. It is ideal when you know the thread on which messages will be published and have control over resource usage.

csharp
public class LocationViewModel : MvxViewModel
{
    private readonly MvxSubscriptionToken _token;

    public LocationViewModel(IMvxMessenger messenger)
    {
        _token = messenger.Subscribe(OnLocationMessage);
    }

    private void OnLocationMessage(LocationMessage locationMessage)
    {
        Lat = locationMessage.Lat;
        Lng = locationMessage.Lng;
    }
}

Subscription on Main Thread

If your message handler needs to interact with the UI, you should use `SubscribeOnMainThread`. This ensures that messages are marshaled to the main UI thread, allowing safe UI updates.

csharp
public class HomeViewModel : MvxViewModel
{
    private readonly MvxSubscriptionToken _token;

    public HomeViewModel(IMvxMessenger messenger)
    {
        _token = messenger.SubscribeOnMainThread(ItemUpdated);
    }

    private void ItemUpdated(UpdatedItemMessage message)
    {
        // Update UI elements here
    }
}

Subscription on ThreadPool Thread

For resource-intensive tasks, use `SubscribeOnThreadPoolThread`. This method queues messages for asynchronous processing on a thread pool thread, preventing UI blocking.

csharp
public class StockPriceViewModel : MvxViewModel
{
    private readonly MvxSubscriptionToken _token;

    public StockPriceViewModel(IMvxMessenger messenger)
    {
        _token = messenger.SubscribeOnThreadPoolThread(UpdateStockPrice);
    }

    private void UpdateStockPrice(StockPriceMessage message)
    {
        // Perform resource-intensive operations here
    }
}

Unsubscribing

Subscriptions can be canceled using the `Unsubscribe` method or by disposing of the `MvxSubscriptionToken`. However, if you rely on weak references, subscriptions will automatically be cleared when the subscriber goes out of scope and is garbage collected.

csharp
public class LogoutService
{
    public void Logout(IMvxMessenger messenger, MvxSubscriptionToken token)
    {
        // Unsubscribe explicitly
        messenger.Unsubscribe(token);
        
        // Alternatively, dispose the token
        // token.Dispose();
    }
}

Using Tags and Reference Types

Subscriptions can also include tags for easier management and inspection of active subscriptions. Additionally, you can specify whether to use weak or strong references to control garbage collection behavior.

csharp
public class MyViewModel : MvxViewModel
{
    private readonly MvxSubscriptionToken _token;

    public MyViewModel(IMvxMessenger messenger)
    {
        // Using a tag for subscription
        _token = messenger.Subscribe(OnMyMessage, "MyTag", MvxReference.Weak);
    }

    private void OnMyMessage(MyMessage message)
    {
        // Handle message
    }
}

These methods provide flexibility in handling messages across different threads and scenarios, making it easier to manage complex application logic in MvvmCross.

Citations:
[1] https://www.mvvmcross.com/documentation/plugins/messenger
[2] https://stackoverflow.com/questions/39315395/mvvmcross-best-way-to-raise-event-in-viewmodel
[3] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[4] https://visualstudiomagazine.com/articles/2018/05/08/build-a-xamarin.forms-application.aspx
[5] https://montemagno.com/ios-android-subscription-implemenation-strategies/
[6] https://canbilgin.wordpress.com/tag/mvvmcross/
[7] https://www.mvvmcross.com/documentation/advanced/mvxnotifytask
[8] http://stackoverflow.com/questions/30848118/capturing-the-onexit-event-in-wpf-with-mvvmcross/31784453
[9] https://stackoverflow.com/questions/51243643/how-to-remove-the-mvvmcross-messenger-subscriptions
[10] https://gregshackles.com/weak-subscription-gotcha-in-mvvmcross/