@@ -136,6 +136,11 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {
136136
137137 // Reset store pagination
138138 storeMessagesEnumerator = store. messagesEnumerator ( forRoom: thread. roomId)
139+
140+ if isLiveTimeline {
141+ hasReachedHomeServerBackwardsPaginationEnd = false
142+ hasReachedHomeServerForwardsPaginationEnd = false
143+ }
139144 }
140145
141146 public func __resetPaginationAroundInitialEvent( withLimit limit: UInt ,
@@ -376,33 +381,38 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {
376381 // MARK: - Private
377382
378383 private func processPaginationResponse( _ response: MXAggregationPaginatedResponse , direction: MXTimelineDirection ) {
379- let dispatchGroup = DispatchGroup ( )
384+ MXLog . debug ( " [MXThreadEventTimeline][ \( self . timelineId ) ] processPaginationResponse: \( response . chunk . count ) messages from the server, direction: \( direction ) " )
380385
381- dispatchGroup. enter ( )
382386 decryptEvents ( response. chunk) {
387+ // process chunk first
383388 for event in response. chunk {
384389 self . addEvent ( event, direction: direction, fromStore: false )
385390 }
386- dispatchGroup. leave ( )
387- }
388- if let rootEvent = response. originalEvent, response. nextBatch == nil {
389- dispatchGroup. enter ( )
390- decryptEvents ( [ rootEvent] ) {
391- self . addEvent ( rootEvent, direction: direction, fromStore: false )
392- dispatchGroup. leave ( )
391+
392+ let updatePaginationInfo = {
393+ switch direction {
394+ case . backwards:
395+ self . backwardsPaginationToken = response. nextBatch
396+ self . hasReachedHomeServerBackwardsPaginationEnd = response. nextBatch == nil
397+ MXLog . debug ( " [MXThreadEventTimeline][ \( self . timelineId) ] processPaginationResponse: hasReachedHomeServerBackwardsPaginationEnd: \( self . hasReachedHomeServerBackwardsPaginationEnd) " )
398+ case . forwards:
399+ self . forwardsPaginationToken = response. nextBatch
400+ self . hasReachedHomeServerForwardsPaginationEnd = response. nextBatch == nil
401+ MXLog . debug ( " [MXThreadEventTimeline][ \( self . timelineId) ] processPaginationResponse: hasReachedHomeServerForwardsPaginationEnd: \( self . hasReachedHomeServerForwardsPaginationEnd) " )
402+ @unknown default :
403+ fatalError ( " [MXThreadEventTimeline][ \( self . timelineId) ] processPaginationResponse: Unknown direction " )
404+ }
393405 }
394- }
395406
396- dispatchGroup. notify ( queue: . main) {
397- switch direction {
398- case . backwards:
399- self . backwardsPaginationToken = response. nextBatch
400- self . hasReachedHomeServerBackwardsPaginationEnd = response. nextBatch == nil
401- case . forwards:
402- self . forwardsPaginationToken = response. nextBatch
403- self . hasReachedHomeServerForwardsPaginationEnd = response. nextBatch == nil
404- @unknown default :
405- fatalError ( " [MXThreadEventTimeline][ \( self . timelineId) ] processPaginationResponse: Unknown direction " )
407+ // then process the root if there is no more messages in server
408+
409+ if let rootEvent = response. originalEvent, response. nextBatch == nil {
410+ self . decryptEvents ( [ rootEvent] ) {
411+ self . addEvent ( rootEvent, direction: direction, fromStore: false )
412+ updatePaginationInfo ( )
413+ }
414+ } else {
415+ updatePaginationInfo ( )
406416 }
407417 }
408418 }
@@ -435,13 +445,8 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline {
435445 if fromStore {
436446 notifyListeners ( event, direction: direction)
437447 } else {
438- if let threadingService = thread. session? . threadingService {
439- threadingService. handleEvent ( event, direction: direction) { handled in
440- if handled {
441- self . notifyListeners ( event, direction: direction)
442- }
443- }
444- }
448+ self . notifyListeners ( event, direction: direction)
449+ thread. session? . threadingService. handleEvent ( event, direction: direction, completion: nil )
445450
446451 if !isLiveTimeline {
447452 store. storeEvent ( forRoom: thread. roomId, event: event, direction: direction)
0 commit comments