From 9ef98509f725ebaae3860f2f6d70b582eac33e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Thu, 14 Nov 2024 09:00:14 +0100 Subject: [PATCH] fix: Use a native swift sort to prevent a crash in Drive --- kDriveCore/Data/Models/Drive/Drive.swift | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/kDriveCore/Data/Models/Drive/Drive.swift b/kDriveCore/Data/Models/Drive/Drive.swift index c6ff0c515..1aeefd808 100644 --- a/kDriveCore/Data/Models/Drive/Drive.swift +++ b/kDriveCore/Data/Models/Drive/Drive.swift @@ -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 } } }