Xamarin forms: Issue with messaging center value passing

SreeeeSreeee INMember ✭✭✭✭✭
edited July 29 in Xamarin.Forms

I am passing an id value through MessagingCenter. The value is passing from Android Mainactivity and subscribed on the PCL ChatPageviewModel. Please see below code:

// Sending group Id through the MessagingCenter on android Mainactivity
MessagingCenter.Send<object, string>(this, "webcontentId", webcontentId);

//Subscribing the group id on PCL ChatPageViewmodel
MessagingCenter.Subscribe<object, string>(this, "webcontentId", (object obj, string notifWebcontentId) => 
{
    //Checking the id values and refreshing the message feed if ids are equal
});

My problem is MessagingCenter.Subscribe code executing multiple times and sometimes it returns old passed id value. I think need to add MessagingCenter.Unsubscribe codes for solving this issue. I added like below code on OnDisappearing method.

 MessagingCenter.Unsubscribe<object, string>(this, "webcontentId");

But after adding this also getting old group id values in MessagingCenter.Subscribe. Is there any mistake on my MessagingCenter.Unsubscribe code? And on where need to add MessagingCenter.Unsubscribe codes?

Answers

  • AlbertKAlbertK MYMember ✭✭✭✭

    Are you calling the MessagingCenter.Subcribe multiple times in you code ?

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlbertK said:
    Are you calling the MessagingCenter.Subcribe multiple times in you code ?

    No, only one MessagingCenter.Subcribe code is in my project.

  • AlbertKAlbertK MYMember ✭✭✭✭

    @Sreeee said:

    @AlbertK said:
    Are you calling the MessagingCenter.Subcribe multiple times in you code ?

    No, only one MessagingCenter.Subcribe code is in my project.

    Is your subscribe done on the constructor of the class or OnCreate ? That will make sure that is only called once

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlbertK I have added the subscribe code on the constructor of the viewmodel class.

  • HarshitaHarshita INMember ✭✭✭✭

    @Sreee
    Unsubscribe your messaging center before subscribe,it will return your newly result.

    and make sure that your are sending new value.Debug your code properly.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @Harshita Is there any mistake you found on my Unsubscribe code.

    MessagingCenter.Unsubscribe<object, string>(this, "webcontentId");
    
  • HarshitaHarshita INMember ✭✭✭✭

    Make sure that your are not subscribing your messaging center multiple times.

Sign In or Register to comment.