Skip to content

Commit

Permalink
docs: Add explanatory comment about .asReversed() when deleting Rea…
Browse files Browse the repository at this point in the history
…lm objects
  • Loading branch information
KevinBoulongne committed Mar 5, 2025
1 parent d15d066 commit 6913618
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class FolderController @Inject constructor(
}

private fun MutableRealm.deleteOutdatedFolders(mailbox: Mailbox, remoteFolders: List<Folder>) {
/**
* This list is reversed because we'll delete items while looping over it.
* Doing so for managed Realm objects will lively update the list we're iterating through, making us skip the next item.
* Looping in reverse enables us to not skip any item.
*/
getFolders(exceptionsFoldersIds = remoteFolders.map { it.id }, realm = this).asReversed().forEach { folder ->
deleteLocalFolder(mailbox, folder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ class MessageController @Inject constructor(private val mailboxContentRealm: Rea
}

fun deleteMessages(context: Context, mailbox: Mailbox, messages: List<Message>, realm: MutableRealm) {
/**
* This list is reversed because we'll delete items while looping over it.
* Doing so for managed Realm objects will lively update the list we're iterating through, making us skip the next item.
* Looping in reverse enables us to not skip any item.
*/
messages.asReversed().forEach { message ->
deleteMessage(context, mailbox, message, realm)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ class RefreshController @Inject constructor(

val message = MessageController.getMessage(uid = shortUid.toLongUid(folderId), realm = this) ?: return@forEach

/**
* This list is reversed because we'll delete items while looping over it.
* Doing so for managed Realm objects will lively update the list we're iterating through, making us skip the next item.
* Looping in reverse enables us to not skip any item.
*/
message.threads.asReversed().forEach { thread ->
scope.ensureActive()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class DraftsActionsWorker @AssistedInject constructor(

var haveAllDraftsSucceeded = true

/**
* This list is reversed because we'll delete items while looping over it.
* Doing so for managed Realm objects will lively update the list we're iterating through, making us skip the next item.
* Looping in reverse enables us to not skip any item.
*/
drafts.asReversed().forEach { draft ->

val isTargetDraft = draft.localUuid == draftLocalUuid
Expand Down

0 comments on commit 6913618

Please sign in to comment.