Messaging Center and Perfomance

Oregon39 156 Reputation points
2021-05-13T23:01:52.877+00:00

Hello:

I'm currently working on a Xamarin Forms application that has latency issues. The application is using Messaging Center to navigate to pages and to pass data around. There are at least one hundred uses of Messaging Center in the app. I've read in a number of online posts that Message Center causes a performance hit and should be used sparingly. My current task is to assess the use of Messaging Center on performance within the app. If anyone has feedback, advice, or useful information on this topic, I'd love to hear your reply.

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-05-18T06:02:27.63+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you just want to pass data, there are some other good ways you could try. For example, the Application subclass has a static Properties dictionary which can be used to store data.

       //store the data  
       Application.Current.Properties ["id"] = someClass.ID;  
         
       //retrive the value  
       if (Application.Current.Properties.ContainsKey("id"))  
       {  
           var id = Application.Current.Properties ["id"] as int;  
           // do something with id  
       }  
    

    Or use Xamarin.Essentials.Preferences to store application preferences in a key/value store.

       //save the value to preferences   
       Preferences.Set("my_key", "my_value");  
         
       //retrieve a value from preferences or a default if not set:  
       var myValue = Preferences.Get("my_key", "default_value");  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2021-05-18T05:41:54.55+00:00

    For "navigation" I think you can use a static class that contains your data so you can use it in all your application and remove messagingcenter.
    Also for "updateLastJCPage" I think you can have a static class that contains this value.


  2. Oregon39 156 Reputation points
    2021-05-17T23:02:01.69+00:00

    @Alessandro Caliaro @JarvanZhang

    Below are some samples of how Messaging Center is used in the app: to navigate, to update data when filtered, keeping track of user location (last page) in the app. There are other ways that the app uses Messaging Center (Biometrics login, authentication, sending messages), but the examples I've provided comprise the highest percentages of Messaging Center use cases.

    \\\\\\used to pass\update data from a filter menu\\\\\\

     else if (_currentPage == JCPage.CitationHistoryView.ToString())  
                {  
                    TimeFilter_Citation.SaveFilter(MyTimeFilter, _filterTypeName);  
      
                    MessagingService.Current.SendMessage("datarefresh", new MessagingServiceDataRefresh  
                    {  
                        DataObjectType = "citation"  
                    });  
      
                    MessagingService.Current.SendMessage("updatefield", new MessagingServiceDataRefresh  
                    {  
                        DataObjectType = "time",  
                        UpdateValue = MyTimeFilter.DisplayName  
                    });  
      
                    if (PopupNavigation.Instance.PopupStack.Count > uint.MinValue) await PopupNavigation.Instance.PopAsync();  
                }  
            }  
      
      
      
      
    \\\\\\\\\\used to navigate\\\\\\\\\\\\  
      
      string url = "CallsDetailView";  
                    string callNum = customPin.Id;  
                    string callsTable = customPin.CallsTableName;  
                    if (!String.IsNullOrEmpty(callNum))  
                        MessagingService.Current.SendMessage("mapdetailsnavigate", new MessagingServiceNavigate  
                        {  
                            URL = url,  
                            NavParameters = new NavigationParameters  
                                {  
                                    {"CallNumber", callNum},  
                                    {"CallsTable", callsTable}  
                                }  
                        });  
      
      
      
     await Task.Run(async () =>  
                {  
                    await Task.Delay(100);  
                    Device.BeginInvokeOnMainThread(async () =>  
                    {  
      
                        await Task.Delay(100);  
                        MessagingService.Current.SendMessage("navigate", new MessagingServiceNavigate  
                        {  
                            URL = url  
                        });  
                    });  
      
                });  
      
      
    \\\\\\\\\used to send data relating to user location in the app\\\\\\\\\\  
      
     public virtual JCPage CurrentJCPage  
            {  
                get { return _currentJCPage; }  
                set  
                {  
                    _currentJCPage = value;  
                    if (CurrentFeatureSection != JCFeatureSection.Auth && CurrentFeatureSection != JCFeatureSection.System && CurrentFeatureSection != JCFeatureSection.Undefined)  
                    {  
                        Settings.Current.LastJCPage = _currentJCPage.ToString();  
      
                        MessagingService.Current.SendMessage("updateLastJCPage", new MessagingServiceDataRefresh  
                        {  
                            DataObjectType = "lastpage",  
                            UpdateValue = _currentJCPage.ToString()  
                        });  
                    }  
      
                    RaisePropertyChanged();  
                }  
            }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.