Question

I am trying to use the TinyMessenger in my iPad application. The iPad application has few UIViewControllers. I would like to see the communication among these controllers via TinyMessenger. I understand the steps as

Step 1 - Create the message hub [ ? ]

var messageHub = new TinyMessengerHub();

Step 2 - Publishing a message [ in UIViewController1]

messageHub.Publish(new MyMessage());

Step 3 - Subscribing to a message [in UIViewController2 ]

messageHub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); });

And MyMessage is defined as below

public class MyMessage : ITinyMessage
{
    /// <summary>
    /// The sender of the message, or null if not supported by the message  implementation.
    /// </summary>
    public object Sender { get; private set; }
}

Please advice if this the correct step to get this setup working. And I dont know where I should create the messagehub. I believe the messagehub has to be global so that it can be accessed by any UIViewController. Can I create messagehub in AppDelegate? If create the messagehub in AppDelegate, how do I access the messagehub from UIViewController1?

Appreciate any help.

Was it helpful?

Solution

You need to use this messenger hub in combination with an IoC container.

You need only 1 instance of the hub within your app, and using a container is the way to do this.

See the example from TinyIoC of setting up the container (which I believe you're using). You would basically call:

var hub = TinyIoCContainer.Current.Resolve<ITinyMessengerHub>();
//for subscribers
hub.Subscribe<YourMessage>(OnYourMessage);
//for publishers
hub.Publish(new YourMessage(this, "BOOYAH!"));

PS - if you are truly using TinyIoC, there is a #if TINYMESSENGER preprocessor directive you can turn on to automatically register a hub for your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top