diff --git a/kDriveCore/Data/Models/Drive/Drive.swift b/kDriveCore/Data/Models/Drive/Drive.swift index df3a24d25..5a67e138b 100644 --- a/kDriveCore/Data/Models/Drive/Drive.swift +++ b/kDriveCore/Data/Models/Drive/Drive.swift @@ -183,9 +183,12 @@ public final class Drive: Object, Codable { return [] } - // 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 fileCategoriesIds: [Int] + if file.isManagedByRealm { + fileCategoriesIds = Array(file.categories.sorted(by: \.addedAt, ascending: true)).map(\.categoryId) + } else { + fileCategoriesIds = file.categories.sorted { $0.addedAt.compare($1.addedAt) == .orderedAscending }.map(\.categoryId) + } let filteredCategories = categories.filter("id IN %@", fileCategoriesIds) // Sort the categories