Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I observe the current subscription status in MvvmCross


How can I observe the current subscription status in MvvmCross


Observing the current subscription status in MvvmCross involves using the `IMvxMessenger` interface, which provides several methods to monitor and manage subscriptions. Here's how you can do it:

Using Subscription Observation Methods

The `IMvxMessenger` API includes several methods to observe the current subscription status:

- `HasSubscriptionsFor()`: This method checks if there are any subscriptions for a specific message type. It returns a boolean indicating whether any subscriptions exist for the given message type.

- `CountSubscriptionsFor()`: This method returns the number of subscriptions for a specific message type.

- `HasSubscriptionsForTag(string tag)`: This method checks if there are any subscriptions for a specific message type with a given tag.

- `CountSubscriptionsForTag(string tag)`: This method returns the number of subscriptions for a specific message type with a given tag.

- `GetSubscriptionTagsFor()`: Although not explicitly mentioned in the provided documentation, this method would typically return a list of tags associated with subscriptions for a specific message type.

Subscribing to `MvxSubscriberChangeMessage`

To dynamically observe changes in subscription status, you can subscribe to `MvxSubscriberChangeMessage` messages. The Messenger itself publishes these messages whenever subscriptions are added, removed, or expire. This allows your application to react to changes in the subscription status in real-time.

Here's an example of how you might subscribe to `MvxSubscriberChangeMessage`:

csharp
var messenger = Mvx.IoCProvider.Resolve();
var token = messenger.Subscribe(OnSubscriberChange);

private void OnSubscriberChange(MvxSubscriberChangeMessage message)
{
    // Handle changes in subscription status here
    // message.MessageType gives you the type of message affected
    // message.SubscriberCount gives you the new count of subscribers
}

Cleanup and Unsubscription

Subscriptions can be cleaned up using the `Unsubscribe` method or by allowing the subscription token to be garbage collected if it is not stored. This is facilitated by the use of weak references in the `MvxSubscriptionToken`, which allows subscriptions to be automatically removed when the subscribing object goes out of scope and is garbage collected[1].

Example Use Case

Here's a basic example of how you might use these methods in a ViewModel:

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

    public MyViewModel(IMvxMessenger messenger)
    {
        _messenger = messenger;
    }

    public void Initialize()
    {
        if (_messenger.HasSubscriptionsFor())
        {
            // Handle existing subscriptions
        }

        _token = _messenger.Subscribe(OnMyMessage);
    }

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

    public override void ViewDisappeared()
    {
        base.ViewDisappeared();
        // Optionally unsubscribe here if needed
        // _token.Dispose();
    }
}

This example shows how to check for existing subscriptions and subscribe to a message type. The subscription token is stored to prevent immediate garbage collection and ensure that messages are received until the ViewModel is disposed of or the token is explicitly disposed of.

Citations:
[1] https://www.mvvmcross.com/documentation/plugins/messenger
[2] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross.Plugins/Messenger/MvxMessengerHub.cs
[3] https://learn.microsoft.com/en-us/sql/relational-databases/replication/monitor/view-publication-and-subscription-status-in-replication-monitor?view=sql-server-ver16
[4] https://stackoverflow.com/questions/35900570/mvvmcross-messenger-subscribing-to-messages-in-static-class
[5] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[6] https://stackoverflow.com/questions/22465026/get-current-viewmodel-mvvmcross
[7] https://www.mvvmcross.com/mvvmcross-6.0.0-release/
[8] https://www.mvvmcross.com/documentation/advanced/mvxnotifytask
[9] https://www.mvvmcross.com/documentation/fundamentals/navigation