From b84c4365515a85fa12d44e811393398b31284c9d Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Thu, 8 Aug 2024 19:04:02 +0200 Subject: [PATCH] Fix for a memory leak (#3143) --- .../Sources/Screens/RoomScreen/RoomScreenViewModel.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift index c437a4ffe..309bca1a7 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift @@ -440,16 +440,14 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol .store(in: &cancellables) Task { [weak self] in - guard let self else { - return - } + // Don't guard let self here, otherwise the for await will strongify the self reference creating a strong reference cycle. // If the subscription has sent a value before the Task has started it might be lost, so before entering the loop we always do an update. - await updatePinnedEventIDs() + await self?.updatePinnedEventIDs() for await _ in roomInfoSubscription.receive(on: DispatchQueue.main).values { guard !Task.isCancelled else { return } - await updatePinnedEventIDs() + await self?.updatePinnedEventIDs() } } .store(in: &cancellables)