CommunityToolkit / CommunityToolkit/dotnet
WeakReferenceMessenger message received multiple times in transient pages
- Lingua principale
- C#
- Stelle
- 3.8k
- Fork
- 400
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
---
**Issue moved from dotnet/maui#14355**
- Please respond to @needledrag.
---
_From @needledrag on Sunday, April 2, 2023 1:48:40 PM_
### Description
When using WeakReferenceMessenger to publish messages, subscribers in pages registered as transient can receive the message multiple times.
This occurs when you load a page registered as transient and in the page register for a message using WeakReferenceMessenger. Initially only one message is received but if you pop the page from the stack and then navigate to it again it registers for the message again.
When the message is published it now receives the message twice.
So popping the page from the stack doesn't destroy its subscriptions which ends up with multiple message being received when you load the page again and a message is published.
### Steps to Reproduce
Using CommunityToolkit.MVVM add an initial Singleton view model and 2 transient view models, Page1, Page2, Page3 respectively.
Subscribe to a basic message in Page1 and navigate to Page2 that subscribes to the same message.
In each message subscription write to console when the message is received.
In Page2 navigate to Page 3 and use WeakReferenceMessenger to publish the message that Page1 and Page 2 receive.
Navigate back to Page 1 and repeat the process.
Page2 receives multiple message the more times you load it and send a message from Page3.
Page 1 (the singleton) only receives a single message each time the message is published.
In supplied example (repo) navigate to second page, then nav to third page and click button, navigate back to first page and repeat.
In the Application output window view the console messages of how many times the messages are recieved in each page.
### Link to public reproduction project repository
https://github.com/needledrag/MVVMDemo.git
### Version with bug
7.0 (current)
### Last version that worked well
Unknown/Other
### Affected platforms
Android, I was *not* able test on other platforms
### Affected platform versions
Android 12
### Did you find any workaround?
I've tried to use WeakReferenceMessenger.Default.IsRegistered to see the page has already subscribed to the message but it always returns false, so I can only assume there is a version of the transient page hanging around in memory that is still subscribed but the current page isn't?
Have been able to work around by subscribing before navigating to sending page and unregistering as soon as message is received.
Supplied example in .net6 but I'm experiencing this in my project in .net 7 too.
### Relevant log output
_No response_
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia con il progetto di riproduzione MVVMDemo fornito e segui la sequenza documentata: naviga tra le pagine transitorie, pubblica da Page3, torna a Page1 e ripeti osservando l’output dell’applicazione. Traccia le registrazioni di WeakReferenceMessenger mentre le pagine transitorie vengono rimosse e ricreate. Il lavoro è completato quando ogni pagina attiva riceve un messaggio per ogni pubblicazione, senza consegne duplicate dopo navigazioni ripetute.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- mobile-dev
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100