From d710dafc94b413112553d16a19999345abad84ed Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 16 May 2023 13:53:27 +0300 Subject: [PATCH] Adopt async uniffi in the RoomSummaryProvider and ensure diffs are still processed serially --- .../RoomSummary/RoomSummaryProvider.swift | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift index 49286e454..7c8f9f803 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift @@ -109,6 +109,19 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { MXLog.info("\(name): Finished applying \(diffs.count) diffs, new room list \(rooms.compactMap { $0.id ?? "Empty" })") } + + // The room summary provider needs to process diffs serially + // Switching to an actor here doesn't help because of all the suspension points + // and actor re-entrance + private static var latestRoomMessage: EventTimelineItem? + private func fetchLastMessage(room: SlidingSyncRoomProtocol) { + let semaphore = DispatchSemaphore(value: 0) + Task { + Self.latestRoomMessage = await room.latestRoomMessage() + semaphore.signal() + } + semaphore.wait() + } private func buildRoomSummaryForIdentifier(_ identifier: String, invalidated: Bool) -> RoomSummary { guard let room = try? slidingSyncListProxy.roomForIdentifier(identifier) else { @@ -119,14 +132,12 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { var attributedLastMessage: AttributedString? var lastMessageFormattedTimestamp: String? - // Dispatch onto another queue otherwise the rust method latestRoomMessage crashes. - // This will be fixed when we get async uniffi support. - DispatchQueue.global(qos: .userInitiated).sync { - if let latestRoomMessage = room.latestRoomMessage() { - let lastMessage = EventTimelineItemProxy(item: latestRoomMessage) - lastMessageFormattedTimestamp = lastMessage.timestamp.formattedMinimal() - attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage) - } + fetchLastMessage(room: room) + + if let latestRoomMessage = Self.latestRoomMessage { + let lastMessage = EventTimelineItemProxy(item: latestRoomMessage) + lastMessageFormattedTimestamp = lastMessage.timestamp.formattedMinimal() + attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage) } let fullRoom = room.fullRoom()