question

Oregon39-5606 avatar image
0 Votes"
Oregon39-5606 asked Oregon39-5606 commented

Messaging Center and Perfomance

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.

dotnet-xamarin
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It's hard to understand why there are >100 messagingcenter in an app... you should post a sample of your code to understand if messagingcenter's calls could be removed

1 Vote 1 ·

Thanks. I'll provide some code samples.

0 Votes 0 ·

Hi, Oregon39. Please post more details about the requirements for using MessagingCenter so that we can help you check if there is a better way.

1 Vote 1 ·

Thanks, I have posted some samples of how Messaging Center is being used in the app.

0 Votes 0 ·

@alessandrocaliaro @JarvanZhang-MSFT

I posted the samples as an answer as the comment section was not taking my comment/code, even as multiple, smaller sections.

0 Votes 0 ·
JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JarvanZhang-MSFT Thanks so much. All the best!

0 Votes 0 ·
Oregon39-5606 avatar image
0 Votes"
Oregon39-5606 answered

@alessandrocaliaro @JarvanZhang-MSFT

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();
             }
         }

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered Oregon39-5606 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks so much for your response.

0 Votes 0 ·