From 8ef6fee95f160b34ea562d95f30391d1ea76dd43 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Thu, 18 Apr 2024 17:21:34 +0300 Subject: [PATCH] Guard against rooms prematurely unsubscribing from sliding sync: - with the advent of permalinks the same room can be now pushed multiple times onto the stack which means multiple calls to `subscribeForUpdates` - the reverse doesn't hold and one call to `unsubscribeFromUpdates` will unsubscribe all the rooms at the same time - we guard against that by tracking the number of subscriptions and calling the underlying `roomListItem.unsubscribe()` when the count goes back to zero --- ElementX/Sources/Services/Room/RoomProxy.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index 107867165..aa0ae28b6 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -21,6 +21,8 @@ import UIKit import MatrixRustSDK class RoomProxy: RoomProxyProtocol { + private static var subscriptionCountPerRoom: [String: Int] = [:] + private let roomListItem: RoomListItemProtocol private let room: RoomProtocol let timeline: TimelineProxyProtocol @@ -89,6 +91,7 @@ class RoomProxy: RoomProxyProtocol { RequiredState(key: "m.room.join_rules", value: "")], timelineLimit: UInt32(SlidingSyncConstants.defaultTimelineLimit)) roomListItem.subscribe(settings: settings) + Self.subscriptionCountPerRoom[roomListItem.id()] = (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) + 1 await timeline.subscribeForUpdates() @@ -98,7 +101,11 @@ class RoomProxy: RoomProxyProtocol { } func unsubscribeFromUpdates() { - roomListItem.unsubscribe() + Self.subscriptionCountPerRoom[roomListItem.id()] = max(0, (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) - 1) + + if Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0 <= 0 { + roomListItem.unsubscribe() + } } lazy var id: String = room.id()