IOS-1459 Reset view model in the background. IOS-1459
authorDirk Zimmermann <dz@pep.security>
Wed, 06 Feb 2019 11:17:23 +0100
branchIOS-1459
changeset 7586df4dfb70941b
parent 7585 16ce1d4c8621
child 7589 3cfe6e22c3f8
IOS-1459 Reset view model in the background.
Backed out changeset 5e226c76121f
pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift
     1.1 --- a/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Wed Feb 06 10:07:58 2019 +0100
     1.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Wed Feb 06 11:17:23 2019 +0100
     1.3 @@ -37,6 +37,12 @@
     1.4      let contactImageTool = IdentityImageTool()
     1.5      let messageSyncService: MessageSyncServiceProtocol
     1.6      internal var messages: SortedSet<MessageViewModel>
     1.7 +    private let queue: OperationQueue = {
     1.8 +        let createe = OperationQueue()
     1.9 +        createe.qualityOfService = .userInitiated
    1.10 +        createe.maxConcurrentOperationCount = 1
    1.11 +        return createe
    1.12 +    }()
    1.13  
    1.14      var lastSearchTerm = ""
    1.15  
    1.16 @@ -125,15 +131,31 @@
    1.17          // Ignore MessageModelConfig.messageFolderDelegate while reloading.
    1.18          self.stopListeningToChanges()
    1.19  
    1.20 -        let messagesToDisplay = self.folderToShow.allMessages()
    1.21 -        let previewMessages = messagesToDisplay.map {
    1.22 -            MessageViewModel(with: $0)
    1.23 +        queue.cancelAllOperations()
    1.24 +        let op = BlockOperation()
    1.25 +        weak var weakOp = op
    1.26 +        op.addExecutionBlock { [weak self] in
    1.27 +            guard
    1.28 +                let me = self,
    1.29 +                let op = weakOp,
    1.30 +                !op.isCancelled else {
    1.31 +                return
    1.32 +            }
    1.33 +            let messagesToDisplay = me.folderToShow.allMessages()
    1.34 +            let previewMessages = messagesToDisplay.map {
    1.35 +                MessageViewModel(with: $0)
    1.36 +            }
    1.37 +            let sortedMessages = SortedSet(array: previewMessages, sortBlock: me.sortByDateSentAscending)
    1.38 +            if op.isCancelled {
    1.39 +                return
    1.40 +            }
    1.41 +            DispatchQueue.main.sync {
    1.42 +                me.messages = sortedMessages
    1.43 +                me.emailListViewModelDelegate?.updateView()
    1.44 +                me.startListeningToChanges()
    1.45 +            }
    1.46          }
    1.47 -        let sortedMessages = SortedSet(array: previewMessages,
    1.48 -                                       sortBlock: self.sortByDateSentAscending)
    1.49 -        self.messages = sortedMessages
    1.50 -        self.emailListViewModelDelegate?.updateView()
    1.51 -        self.startListeningToChanges()
    1.52 +        queue.addOperation(op)
    1.53      }
    1.54  
    1.55      // MARK: - Public Data Access & Manipulation