CommunityToolkit / CommunityToolkit/dotnet

WeakReferenceMessenger message received multiple times in transient pages

Open
#657 15 comments 4 reactions 0 assignees View on GitHub
mvvm-toolkit :toolbox: need more info :pushpin:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

---
**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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.