2

I'm experiencing an annoying issue with xamarin messaging center. I added some break points and noticed that only one message is sent but the subscriber receives it twice.

My sender code(Page2):

async void CompartilhaMapa(System.Object sender, System.EventArgs e)
{ 

    ...        

   MessagingCenter.Send<Page2, ParamType>(this, "PopUpData", new ParamType() { Tipo = 2, Valor = Coords });
   Console.WriteLine("msg sent"); //displayed only once and line above has a break line assuring this
   await Navigation.PopPopupAsync();
}

Page1(Subscriber):

protected override void OnAppearing()
{
   base.OnAppearing();
   MessagingCenter.Subscribe<Page2, ParamType>(this, "PopUpData", async (sender, arg) =>
   {
      Task.WaitAll(tasks.ToArray());
      switch (arg.Tipo)
      {
          case 2:
             Console.WriteLine(" *********** msg received"); //this is outputted twice
             tasks.Add(Task.Run(() => ShareMap(arg))); //this is called twice
             break;
             case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
             case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
      }
    });
}

protected override void OnDisappearing()
{
   base.OnDisappearing();
   MessagingCenter.Unsubscribe<Page2, ParamType>(this, "PopUpData");
}

What I'm doing wrong here?

Éder Rocha
  • 1,538
  • 11
  • 29
  • Is this Android or iOS? – Mihail Duchev Jun 09 '20 at 14:13
  • @MihailDuchev android – Éder Rocha Jun 09 '20 at 14:14
  • 1
    This is strange. Usually it happens on iOS, since there OnDisappearing doesn't get call the same ways it gets called on Android. Here the issue is that you have multiple subscriptions for some reason. What I can suggest is to always unsubscribe before subscribing. This way, you will be sure that you have only 1 instance. Also, you can move the subscribe logic in the ctor, if you don't explicitly need it on Appearing. This way, your ctor will look like this: `ctor() { MessagingCenter.Unsubscribe..... MessagingCenter.Subscribe }` This way you will know for sure that you will have only 1 instance – Mihail Duchev Jun 09 '20 at 14:18
  • @MihailDuchev the strange is that it doesn't occurs always. Sometimes it receives only once, sometimes twice or three times. But will try what you said in your last answer. – Éder Rocha Jun 09 '20 at 14:25
  • @MihailDuchev just did the changes but same issue. – Éder Rocha Jun 09 '20 at 14:30
  • Can you please share us a Minimal, Reproducible Example? You code looks fine and it hard for us to reproduce the issue with those codes. – nevermore Jun 10 '20 at 01:48
  • Is there more than one page1 or page2 instance exist in your project at that time you call `CompartilhaMapa`? – nevermore Jun 10 '20 at 02:56
  • @JackHua-MSFT we only can see that page when the others were closed. – Éder Rocha Jun 10 '20 at 18:04
  • Need a Minimal, Reproducible Example to find out the cause. – nevermore Jun 11 '20 at 05:37

1 Answers1

-2

There are two possible reasons for this:

Reason 1 - The message is being sent more than once. Since you are insisting that it is sending only once, so I will ignore this. I still suggest that you should place a break point in your CompartilhaMapa function (sender code), to make sure that the function is in fact only called once.

Reason 2 - You have subscribed to the event more than once. To fix this,

  1. Place the subscribe event inside the constructor of page1 instead of inside the OnAppearing.
public Page1()
{
    ...
    MessagingCenter.Subscribe<Page2, ParamType>(this, "PopUpData", async (sender, arg) => ...

}
  1. Place the unsubscribe event inside the destructor of Page1 instead of inside the OnDisappearing. (You can even place this inside the OnBackButtonPressed override function instead of Page1)
~Page1()
{
    ...
    MessagingCenter.Unsubscribe<Page2, ParamType>(this, "PopUpData")
}

I have assumed Page1 is the name of the page, just replace it with the actual name.


Workaround - You have subscribed to the event more than once. To fix this,

  1. Place the unsubscribe event before any use of the subscribe event
public void SampleFunction()
{
    ...
    MessagingCenter.Unsubscribe<Page2, ParamType>(this, "PopUpData")
    MessagingCenter.Subscribe<Page2, ParamType>(this, "PopUpData", async (sender, arg) => ...

}
Saamer
  • 4,687
  • 1
  • 13
  • 55