Skip to content

Commit

Permalink
Merge pull request #1340 from Infomaniak/fix-realm-drive-sort-crash
Browse files Browse the repository at this point in the history
fix: Use a native swift sort to prevent a crash in Drive
  • Loading branch information
Ambrdctr authored Nov 14, 2024
2 parents 33bcf74 + 4039936 commit 84b9f06
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions kDriveCore/Data/Models/Drive/Drive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,11 @@ public final class Drive: Object, Codable {
return []
}

let fileCategoriesIds: [Int]
if file.isManagedByRealm {
fileCategoriesIds = Array(file.categories.sorted(by: \.addedAt, ascending: true)).map(\.categoryId)
} else {
// File is not managed by Realm: cannot use the `.sorted(by:)` method :(
fileCategoriesIds = file.categories.sorted { $0.addedAt.compare($1.addedAt) == .orderedAscending }.map(\.categoryId)
}
let filteredCategories = categories.filter(NSPredicate(format: "id IN %@", fileCategoriesIds))
// If File is not managed by Realm: cannot use the `.sorted(by:)` method :(
// Also the Realm sort can crash if managed by realm
let fileCategoriesIds = file.categories.sorted { $0.addedAt.compare($1.addedAt) == .orderedAscending }.map(\.categoryId)
let filteredCategories = categories.filter("id IN %@", fileCategoriesIds)

// Sort the categories
return fileCategoriesIds.compactMap { id in filteredCategories.first { $0.id == id } }
}
Expand Down

0 comments on commit 84b9f06

Please sign in to comment.