Skip to content

Commit

Permalink
Merge pull request #960 from Infomaniak/hardenThreadGeneration
Browse files Browse the repository at this point in the history
feat: use ApplicationBackgroundTaskTracker to harden thread creation
  • Loading branch information
Ambrdctr committed Sep 4, 2023
2 parents 36efd26 + 63af095 commit 847777e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MailCore/Cache/MailboxManager/MailboxManager+Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public extension MailboxManager {
messageUids: uniqueUids
)

let backgroundTracker = await ApplicationBackgroundTaskTracker(identifier: #function + UUID().uuidString)
await backgroundRealm.execute { [self] realm in
if let folder = folder.fresh(using: realm) {
createThreads(messageByUids: messageByUidsResult, folder: folder, using: realm)
Expand All @@ -319,6 +320,7 @@ public extension MailboxManager {
newCursor: newCursor
)
}
await backgroundTracker.end()
}

private func createThreads(messageByUids: MessageByUidsResult, folder: Folder, using realm: Realm) {
Expand Down Expand Up @@ -426,6 +428,7 @@ public extension MailboxManager {
private func updateMessages(updates: [MessageFlags], folder: Folder) async {
guard !Task.isCancelled else { return }

let backgroundTracker = await ApplicationBackgroundTaskTracker(identifier: #function + UUID().uuidString)
await backgroundRealm.execute { realm in
var threadsToUpdate = Set<Thread>()
try? realm.safeWrite {
Expand All @@ -446,6 +449,7 @@ public extension MailboxManager {
self.updateThreads(threads: threadsToUpdate, realm: realm)
}
}
await backgroundTracker.end()
}

private func updateThreads(threads: Set<Thread>, realm: Realm) {
Expand Down
3 changes: 3 additions & 0 deletions MailCore/Cache/MailboxManager/MailboxManager+Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation
import InfomaniakCore
import InfomaniakCoreUI
import RealmSwift

// MARK: - Thread
Expand Down Expand Up @@ -51,6 +52,7 @@ public extension MailboxManager {
internal func deleteMessages(uids: [String]) async {
guard !uids.isEmpty && !Task.isCancelled else { return }

let backgroundTracker = await ApplicationBackgroundTaskTracker(identifier: #function + UUID().uuidString)
await backgroundRealm.execute { realm in
let batchSize = 100
for index in stride(from: 0, to: uids.count, by: batchSize) {
Expand Down Expand Up @@ -96,6 +98,7 @@ public extension MailboxManager {
}
}
}
await backgroundTracker.end()
}

internal func saveThreads(result: ThreadResult, parent: Folder) async {
Expand Down
1 change: 1 addition & 0 deletions MailCore/Models/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class Thread: Object, Decodable, Identifiable {
answered = messages.map(\.answered).contains(true)
forwarded = messages.map(\.forwarded).contains(true)

// Re-ordering of messages in a thread
messages = messages.sorted {
$0.date.compare($1.date) == .orderedAscending
}.toRealmList()
Expand Down

0 comments on commit 847777e

Please sign in to comment.