diff --git a/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift b/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift index ee717ddf33..00cece42d3 100644 --- a/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift +++ b/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift @@ -136,6 +136,11 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline { // Reset store pagination storeMessagesEnumerator = store.messagesEnumerator(forRoom: thread.roomId) + + if isLiveTimeline { + hasReachedHomeServerBackwardsPaginationEnd = false + hasReachedHomeServerForwardsPaginationEnd = false + } } public func __resetPaginationAroundInitialEvent(withLimit limit: UInt, @@ -376,33 +381,38 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline { // MARK: - Private private func processPaginationResponse(_ response: MXAggregationPaginatedResponse, direction: MXTimelineDirection) { - let dispatchGroup = DispatchGroup() + MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: \(response.chunk.count) messages from the server, direction: \(direction)") - dispatchGroup.enter() decryptEvents(response.chunk) { + // process chunk first for event in response.chunk { self.addEvent(event, direction: direction, fromStore: false) } - dispatchGroup.leave() - } - if let rootEvent = response.originalEvent, response.nextBatch == nil { - dispatchGroup.enter() - decryptEvents([rootEvent]) { - self.addEvent(rootEvent, direction: direction, fromStore: false) - dispatchGroup.leave() + + let updatePaginationInfo = { + switch direction { + case .backwards: + self.backwardsPaginationToken = response.nextBatch + self.hasReachedHomeServerBackwardsPaginationEnd = response.nextBatch == nil + MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: hasReachedHomeServerBackwardsPaginationEnd: \(self.hasReachedHomeServerBackwardsPaginationEnd)") + case .forwards: + self.forwardsPaginationToken = response.nextBatch + self.hasReachedHomeServerForwardsPaginationEnd = response.nextBatch == nil + MXLog.debug("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: hasReachedHomeServerForwardsPaginationEnd: \(self.hasReachedHomeServerForwardsPaginationEnd)") + @unknown default: + fatalError("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: Unknown direction") + } } - } - dispatchGroup.notify(queue: .main) { - switch direction { - case .backwards: - self.backwardsPaginationToken = response.nextBatch - self.hasReachedHomeServerBackwardsPaginationEnd = response.nextBatch == nil - case .forwards: - self.forwardsPaginationToken = response.nextBatch - self.hasReachedHomeServerForwardsPaginationEnd = response.nextBatch == nil - @unknown default: - fatalError("[MXThreadEventTimeline][\(self.timelineId)] processPaginationResponse: Unknown direction") + // then process the root if there is no more messages in server + + if let rootEvent = response.originalEvent, response.nextBatch == nil { + self.decryptEvents([rootEvent]) { + self.addEvent(rootEvent, direction: direction, fromStore: false) + updatePaginationInfo() + } + } else { + updatePaginationInfo() } } } @@ -435,13 +445,8 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline { if fromStore { notifyListeners(event, direction: direction) } else { - if let threadingService = thread.session?.threadingService { - threadingService.handleEvent(event, direction: direction) { handled in - if handled { - self.notifyListeners(event, direction: direction) - } - } - } + self.notifyListeners(event, direction: direction) + thread.session?.threadingService.handleEvent(event, direction: direction, completion: nil) if !isLiveTimeline { store.storeEvent(forRoom: thread.roomId, event: event, direction: direction) diff --git a/changelog.d/5840.bugfix b/changelog.d/5840.bugfix new file mode 100644 index 0000000000..bf8d888c4a --- /dev/null +++ b/changelog.d/5840.bugfix @@ -0,0 +1 @@ +MXThreadEventTimeline: Fix processing order of thread events & fix empty thread screen issue.