diff --git a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift index 12c12a918b..f303664067 100644 --- a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift @@ -58,7 +58,12 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol { actionsSubject.eraseToAnyPublisher() } - private var roomProxy: RoomProxyProtocol? + private var roomProxy: RoomProxyProtocol? { + didSet { + oldValue?.unsubscribeFromUpdates() + } + } + private var timelineController: RoomTimelineControllerProtocol? init(userSession: UserSessionProtocol, diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index 74697edd80..12b5e21a3e 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -1864,6 +1864,18 @@ class RoomProxyMock: RoomProxyProtocol { subscribeForUpdatesCallsCount += 1 await subscribeForUpdatesClosure?() } + //MARK: - unsubscribeFromUpdates + + var unsubscribeFromUpdatesCallsCount = 0 + var unsubscribeFromUpdatesCalled: Bool { + return unsubscribeFromUpdatesCallsCount > 0 + } + var unsubscribeFromUpdatesClosure: (() -> Void)? + + func unsubscribeFromUpdates() { + unsubscribeFromUpdatesCallsCount += 1 + unsubscribeFromUpdatesClosure?() + } //MARK: - redact var redactCallsCount = 0 diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index f5f69dbe76..19c15afca6 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -51,10 +51,6 @@ class RoomProxy: RoomProxyProtocol { room.ownUserId() } - deinit { - roomListItem.unsubscribe() - } - init(roomListItem: RoomListItemProtocol, room: RoomProtocol, backgroundTaskService: BackgroundTaskServiceProtocol) async { @@ -89,6 +85,10 @@ class RoomProxy: RoomProxyProtocol { subscribeToRoomStateUpdates() } + + func unsubscribeFromUpdates() { + roomListItem.unsubscribe() + } lazy var id: String = room.id() diff --git a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift index 321edf9185..5497bfc61e 100644 --- a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift +++ b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift @@ -69,6 +69,8 @@ protocol RoomProxyProtocol { func subscribeForUpdates() async + func unsubscribeFromUpdates() + func redact(_ eventID: String) async -> Result func reportContent(_ eventID: String, reason: String?) async -> Result