Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use a native swift sort to prevent a crash in Drive #1340

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading