ChatChannelVC marks messages delivered while being dismissed as read
What did you do?
I've built some UI that badges the button that leads you to Stream chat when the chat has an unread message in it.
What did you expect to happen?
When receiving a message while dismissing the Stream chat, I expected the Stream chat to not mark that message as read
What happened instead?
Stream marks messages are read while the Stream UI is being dismissed. This is because DefaultViewVisibilityChecker says the view controller is on screen even when it is being dismissed.
GetStream Environment
GetStream Chat version: 4.64.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 18 Swift version: 6 Xcode version: 16 Device: all
Additional context
I could fix address this easily myself if shouldMarkChannelRead were open rather than public. As-is I think I need to override channelController(_:didUpdateMessages:) which is much more involved.
Relatedly, markReadThrottler.execute should probably cancel enqueued actions when the VC goes off screen
Hi @dfed ,
We had a similar issue reported here before: https://github.com/GetStream/stream-chat-swift/issues/3089
Make sure that there are no memory leaks in your app, and that the controller is not kept alive before leaving the screen.
Either way, we are going to discuss internally to see if we can improve this. We will let you know once we have a conclusion, but please meanwhile check that issue just to double if you are not doing anything wrong.
Best, Nuno
Oh! @nuno-vieira I am explicitly keeping the ChatChannelVC and ChatChannelController alive after the view is dismissed so that:
- the chat's scroll view position is not lost when a user navigates back
- I can get new updates events for new messages – I'm driving this off of
channelController(_:didUpdateChannel:)calls
If there's a better way to achieve the above lmk!
Hi @dfed,
We are considering exposing shouldMarkChannelRead, but before that could please explain a bit better how is the ChatChannelVC kept alive? Can you maybe share a video of your UX? You can send to [email protected], if you don't want to share it here.
Just for us to understand if there is a better way to do things. Because even if ChatChannelVC is kept alive, the DefaultViewVisibilityChecker should not say that the screen is visible if it is not in the window.
You can also try to access the ChatChannelVC.markReadThrottler and call cancel() whenever you dismiss it and keep it alive.
Best, Nuno
please explain a bit better how is the ChatChannelVC kept alive?
Happily @nuno-vieira! Video here
And our code is simple enough. Within our navigation controller which pushes/pops this VC:
// We keep a reference to the textViewController around even when it's off screen to preserve scroll position and text field content.
private lazy var textViewController = textViewControllerBuilder.instantiate(userChannel)
Where textViewControllerBuilder is a builder type that returns our subclass of ChatChannelVC.
Just for us to understand if there is a better way to do things. Because even if
ChatChannelVCis kept alive, theDefaultViewVisibilityCheckershould not say that the screen is visible if it is not in the window.
Correct! My issue is that DefaultViewVisibilityChecker should not say that the screen is visible while the screen is being dismissed. Once the screen completes dismissal, DefaultViewVisibilityChecker correctly says the view is not on screen.
You can also try to access the
ChatChannelVC.markReadThrottlerand callcancel()whenever you dismiss it and keep it alive.
A few things here:
markReadThrottlerisinternal, so I can't access itcancel()is already being called inviewWillAppear()- Ideally we'd call
cancel()again inviewDidDisappear()to ensure we don't mark messages received betweenviewWillAppear()andviewDidDisappear()as read.
We're using Stream to chat with an AI agent, and the agent is fast. Meaning that it's incredibly likely that if you send a message and immediately dismiss the Stream chat that you'll get a message back between viewWillAppear() and viewDidDisappear().
We are considering exposing
shouldMarkChannelRead
Great! Making this property open would absolutely help.
I see @dfed,
We will make the property open then 👍 Here is the PR: https://github.com/GetStream/stream-chat-swift/pull/3468
It should come on the next release, ETA is about 2 weeks.
Best, Nuno
This is now fixed on the latest release! 🚀
Thank you for your patience!