Skip to content

Commit

Permalink
fix: Remove oldUids by value instead of range (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann authored Aug 27, 2024
2 parents 4cb6872 + d92c429 commit 1c23e7f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
23 changes: 13 additions & 10 deletions MailCore/Cache/MailboxManager/MailboxManager+Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public extension MailboxManager {
return
}

folder.oldMessagesUidsToFetch = messageUidsResult.messageShortUids.toRealmList()
folder.oldMessagesUidsToFetch = messageUidsResult.messageShortUids.map { MessageUid(uid: $0) }.toRealmList()
}

return messageUidsResult.cursor
Expand All @@ -175,12 +175,13 @@ public extension MailboxManager {
!liveFolder.newMessagesUidsToFetch.isEmpty else { return false }

let range: Range = 0 ..< min(liveFolder.newMessagesUidsToFetch.count, Constants.newPageSize)
let nextUids: [String] = Array(liveFolder.newMessagesUidsToFetch[range])
let nextUids: [String] = liveFolder.newMessagesUidsToFetch[range].map { $0.uid }
try await addMessages(shortUids: nextUids, folder: folder)

try writeTransaction { writableRealm in
let freshFolder = folder.fresh(using: writableRealm)
freshFolder?.newMessagesUidsToFetch.removeSubrange(range)
guard let freshFolder = folder.fresh(using: writableRealm) else { return }
let uidsToRemove = freshFolder.newMessagesUidsToFetch.where { $0.uid.in(nextUids) }
writableRealm.delete(uidsToRemove)
}

return true
Expand All @@ -199,14 +200,16 @@ public extension MailboxManager {
var newThreadsCount = 0

let range: Range = 0 ..< min(liveFolder.oldMessagesUidsToFetch.count, Constants.oldPageSize)
let nextUids: [String] = Array(liveFolder.oldMessagesUidsToFetch[range])
let nextUids: [String] = liveFolder.oldMessagesUidsToFetch[range].map { $0.uid }
try await addMessages(shortUids: nextUids, folder: folder)

try? writeTransaction { writableRealm in
let freshFolder = folder.fresh(using: writableRealm)
freshFolder?.oldMessagesUidsToFetch.removeSubrange(range)
freshFolder?.remainingOldMessagesToFetch -= Constants.oldPageSize
newThreadsCount = freshFolder?.threads.count ?? 0
guard let freshFolder = folder.fresh(using: writableRealm) else { return }
let uidsToRemove = freshFolder.oldMessagesUidsToFetch.where { $0.uid.in(nextUids) }
writableRealm.delete(uidsToRemove)

freshFolder.remainingOldMessagesToFetch -= Constants.oldPageSize
newThreadsCount = freshFolder.threads.count
}

return newThreadsCount - threadsCount
Expand All @@ -227,7 +230,7 @@ public extension MailboxManager {
// Add Uids to fetch in the folder
try? writeTransaction { writableRealm in
let freshFolder = folder.fresh(using: writableRealm)
freshFolder?.newMessagesUidsToFetch.append(objectsIn: messageUids.addedShortUids)
freshFolder?.newMessagesUidsToFetch.append(objectsIn: messageUids.addedShortUids.map { MessageUid(uid: $0) })

if let newUnreadCount = messageUids.folderUnreadCount {
freshFolder?.remoteUnreadCount = newUnreadCount
Expand Down
9 changes: 5 additions & 4 deletions MailCore/Cache/MailboxManager/MailboxManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class MailboxManager: ObservableObject, MailboxManageable {
let realmName = "\(mailbox.userId)-\(mailbox.mailboxId).realm"
realmConfiguration = Realm.Configuration(
fileURL: MailboxManager.constants.rootDocumentsURL.appendingPathComponent(realmName),
schemaVersion: 33,
schemaVersion: 34,
migrationBlock: { migration, oldSchemaVersion in
// No migration needed from 0 to 16
if oldSchemaVersion < 17 {
Expand Down Expand Up @@ -111,7 +111,8 @@ public final class MailboxManager: ObservableObject, MailboxManageable {
Attendee.self,
Bimi.self,
SwissTransferAttachment.self,
File.self
File.self,
MessageUid.self
]
)

Expand Down Expand Up @@ -191,8 +192,8 @@ public final class MailboxManager: ObservableObject, MailboxManageable {
folder.lastUpdate = savedFolder.lastUpdate
folder.cursor = savedFolder.cursor
folder.remainingOldMessagesToFetch = savedFolder.remainingOldMessagesToFetch
folder.oldMessagesUidsToFetch = savedFolder.oldMessagesUidsToFetch
folder.newMessagesUidsToFetch = savedFolder.newMessagesUidsToFetch
folder.oldMessagesUidsToFetch = savedFolder.oldMessagesUidsToFetch.detached()
folder.newMessagesUidsToFetch = savedFolder.newMessagesUidsToFetch.detached()
folder.isExpanded = savedFolder.isExpanded
}

Expand Down
17 changes: 15 additions & 2 deletions MailCore/Models/Folder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public enum ToolFolderType: String, PersistableEnum {
case search
}

public class MessageUid: EmbeddedObject {
@Persisted public var uid: String

override public init() {
super.init()
}

public convenience init(uid: String) {
self.init()
self.uid = uid
}
}

public class Folder: Object, Codable, Comparable, Identifiable {
@Persisted(primaryKey: true) public var remoteId: String
@Persisted public var path: String
Expand All @@ -121,9 +134,9 @@ public class Folder: Object, Codable, Comparable, Identifiable {
/// List of old Messages UIDs of this Folder that we need to fetch.
/// When first opening the Folder, we get the full list of UIDs, and we store it.
/// Then, we'll be able to go through it as we want to fetch the old Messages.
@Persisted public var oldMessagesUidsToFetch: RealmSwift.List<String>
@Persisted public var oldMessagesUidsToFetch: RealmSwift.List<MessageUid>
/// List of new Messages UIDs of this Folder that we need to fetch.
@Persisted public var newMessagesUidsToFetch: RealmSwift.List<String>
@Persisted public var newMessagesUidsToFetch: RealmSwift.List<MessageUid>
@Persisted public var isExpanded = true

/// Date of last threads update
Expand Down
3 changes: 2 additions & 1 deletion MailTests/Folders/FolderStructureGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ final class InMemoryRealmAccessor: RealmAccessible {
SubBody.self,
CalendarEvent.self,
CalendarEventResponse.self,
SwissTransferAttachment.self
SwissTransferAttachment.self,
MessageUid.self
])

// It's a unit test
Expand Down

0 comments on commit 1c23e7f

Please sign in to comment.