MessagingCenter implementation making my app slow

I have implemented pub sub pattern of MessagingCenter around my app but after implementing this pattern I found my app becomes very slow. But I need to implement this, can you suggest what should I do now?

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @ShahriatHossain - When you say your app "becomes" very slow, is that straight away, or does it get slower over time?

    If it gets slower over time, you probably have a memory leak.

    If it gets slower straight away, do you have code unnecessarily blocking the UI thread, or do you have propagation of messages such that rather than sending N messages, you are actually sending N-factorial or similar?

  • ShahriatHossainShahriatHossain USMember ✭✭

    @JohnHardman said:
    @ShahriatHossain - When you say your app "becomes" very slow, is that straight away, or does it get slower over time?

    If it gets slower over time, you probably have a memory leak.

    If it gets slower straight away, do you have code unnecessarily blocking the UI thread, or do you have propagation of messages such that rather than sending N messages, you are actually sending N-factorial or similar?

    Basically I am using xamarin pub sub pattern on app level when app goes to background mode to refresh a specific page as you can see my code:

    protected override void OnResume()
            {
                MessagingCenter.Send<App>(this, "RefreshPage");
            }
    

    and subscribing this "RefreshPage" message on my PageViewModel1 to refresh that page to load latest data from api:

    public void RefreshPageWhenAppResume()
            {
                MessagingCenter.Subscribe<App>(this, "RefreshPage", async (sender) =>
                {
                    await this.PopulateList();
                    this.CheckNetworkAndChannelAvailability();
                });
            }
    

    I am unsubscribing this "RefreshPage" on this ViewModel1 like below:

    public void OnNavigatedFrom(NavigationParameters parameters)
            {
                this.SelectedItem= null;
                MessagingCenter.Unsubscribe<App>(this, "RefreshPage");
            }
    

    I have another use of MessagingCenter for push notification like below:
    // methods for SubscribeViewModel
    private async void Subscribe()
    {
    MessagingCenter.Send(this, "ItemSubscribed", ItemId.ToString());
    }

    // methods for UnSubscribeViewModel
    private async void UnSubscribe()
    {
        MessagingCenter.Send(this, "ItemUnSubscribed", ItemId.ToString());
    }
    

    and I am subscribing these two messages on OnCreate method of MainActivity class for android:

    Xamarin.Forms.MessagingCenter.Subscribe<ViewModels.SubscribeViewModel, string>(this, "ItemSubscribed", (sender, arg) =>
                    {
                        StartIntentService();
                    });
    
                    Xamarin.Forms.MessagingCenter.Subscribe<ViewModels.UnSubscribeViewModel, string>(this, "ItemUnSubscribed", (sender, arg) =>
                    {
                        StartIntentService();
                    });
    
    private void StartIntentService()
            {
                var intent = new Intent(this, typeof(RegistrationIntentService));
                StartService(intent);
            }
    

    On MainActivity class I am not unsubscribing those messages though. So here is my all above scenarios of using xamarin pub sub pattern. Please let me know what I have done wrong which are causes of memory leaks?

  • ShahriatHossainShahriatHossain USMember ✭✭

    @JohnHardman said:
    @ShahriatHossain - When you say your app "becomes" very slow, is that straight away, or does it get slower over time?

    If it gets slower over time, you probably have a memory leak.

    If it gets slower straight away, do you have code unnecessarily blocking the UI thread, or do you have propagation of messages such that rather than sending N messages, you are actually sending N-factorial or similar?

    Basically I am using MessagingCenter class in two scenarios like on app level:
    // App.cs
    protected override void OnResume()
    {
    MessagingCenter.Send(this, "RefreshPage");
    }

    and subscribing this "RefreshPage" message on a ViewModelPage:

        public void RefreshPageWhenAppResume()
                {
                    MessagingCenter.Subscribe<App>(this, "RefreshPage", async (sender) =>
                    {
                        await this.PopulateItemList();
                        this.CheckNetworkAvailability();
                    });
                }
    

    and also unsubscribing:

    public void OnNavigatedFrom(NavigationParameters parameters)
            {
                this.SelectedItem = null;
                MessagingCenter.Unsubscribe<App>(this, "RefreshPage");
            }
    

    I am also calling MessagingCenter when subscribe and unsubscribe an item for GCM push notification:
    //SubscribeViewModel
    private async void Subscribe()
    {
    MessagingCenter.Send(this, "ItemSubscribed", ItemId.ToString());
    }

    //UnSubscribeViewModel
    private async void UnSubscribe()
    {
    MessagingCenter.Send(this, "ItemUnSubscribed", ItemId.ToString());
    }

    and subscribing these two message on OnCreate method for MainActivity class for android:

    Xamarin.Forms.MessagingCenter.Subscribe<ViewModels.SubscribeViewModel, string>(this, "ItemSubscribed", (sender, arg) =>
                    {
                        StartIntentService();
                    });
    
                    Xamarin.Forms.MessagingCenter.Subscribe<ViewModels.UnSubscribeViewModel, string>(this, "ItemUnSubscribed", (sender, arg) =>
                    {
                        StartIntentService();
                    });
    
    private void StartIntentService()
            {
                var intent = new Intent(this, typeof(RegistrationIntentService));
                StartService(intent);
            }
    

    on MainActivity class I am not unsubscribing those two messages, so not sure where this could be reason of making app slow?

  • JohnHardmanJohnHardman GBUniversity mod

    @ShahriatHossain - When you say your app "becomes" very slow, is that straight away, or does it get slower over time?

  • ShahriatHossainShahriatHossain USMember ✭✭

    @JohnHardman said:
    @ShahriatHossain - When you say your app "becomes" very slow, is that straight away, or does it get slower over time?

    it gets slower overtime and app crashing.

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2017

    @ShahriatHossain - OK, so probably a memory leak.

    Have you tried profiling the app to see if there are objects not being released?

    Have you put trace code in to see how often your message handlers are executed?

    Have you tried narrowing things down by only having one subscription in use?

    Have you tried removing any await'ed lines in your message handlers?

Sign In or Register to comment.