Any disadvantages to using MessagingCenter?

PhilipOGormanPhilipOGorman USMember ✭✭✭

I've been writing apps using Forms for over a year now - but I haven't used MessagingCenter yet.
I am using the standard .Net way of sending and subscribing to events. If I need to subscribe to an event I need to have a reference to the object that is raising it.
From what I see MessagingCenter could completely decouple objects and allow communication between them.

The disadvantages I see are:
1) The classes will have a dependency to Xamarin.Forms (which is fine for ViewModels - they probably already depend on XF )
2) Unit testing is made difficult - how could Messages be sent when testing? Would MessagingCenter have to wrapped in order to test?

Is it worth using MessagingCenter - am I missing out?

Posts

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    In some situations where you don't have direct access to another object it's really great. I use it to communicate in some cases from a CodebehindFile to a ViewModel. Also to communicate that Exception where Catched it can be used.
    What I don't like are the Magic Strings. My Best Apporach was to use static string constants vor this

  • MaharshiChoudhuryMaharshiChoudhury INMember ✭✭✭

    you might have to unsubscribe the messaging center when you are done with your page

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @MaharshiChoudhury

    Yep, like behaviors, bindingcontext etc, it all has to be cleaned up or it hangs about in memory..

    I try and limit my use of it, usually have a class for each message type, no magic strings.

    using System;
    
    namespace XamFormsTestApp.Data.Messages
    {
        public class UpdateNotificationsMessage
        {
            public static readonly string MessageId = Guid.NewGuid().ToString();
        }
    }
    
    public StartViewModel(IOrderService ordServ, INavService nav, IDialogService dialog)
            {
                _ordService = ordServ;
                _navService = nav;
                _dialogService = dialog;
    
                Messenger.Default.Register<UpdateNotificationsMessage>(this, action =>
                {
                    // Do something
                });
            }
    

    And when the bindingcontext is cleared the VM cleans up it's resources

    public override void Cleanup()
            {
                Messenger.Default.Unregister<UpdateNotificationsMessage>(this);
            }
    
  • PhilipOGormanPhilipOGorman USMember ✭✭✭

    Thanks guys, good points. Another question, does the messaging center message handlers always run on the UI thread?

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    Think they run on the thread that they were instantiated on.

  • HunumanHunuman GBMember ✭✭✭✭
    edited June 2016

    @PhilipOGorman

    You might want to bear @TheRealJasonSmith 's advice re MessagingCenter in mind.

    To summarise:

    MessagingCenter - Prefer to use something else (e.g. Prism).
    Do pass MessagingCenter either a static or instance method, not a closure/lambda expression.

    Have a look at this cheat sheet for more of Jason's excellent advice.
    http://kent-boogaart.com/blog/jason-smith's-xamarin-forms-performance-tips

    Hope this helps,

    Tim

  • Jesse_JiangJesse_Jiang USMember ✭✭✭

    I used Rx.net to send message and receive message, it don't need to unsubscribe, and also can do Linq for message.

  • PhilipOGormanPhilipOGorman USMember ✭✭✭

    rx.net looks good!
    I wonder why @TheRealJasonSmith does not recommend MessagingCenter..?

  • HunumanHunuman GBMember ✭✭✭✭

    @PhilipOGorman

    I think it is due to the threading issue....apparently messages are not marshalled across thread boundaries.
    Hope someone else can confirm or refute this, it would be good to know for sure.

    Tim

Sign In or Register to comment.