Skip to content

Commit

Permalink
Merge pull request #39 from boscojwho/bosco/fix-calculate-concurrency…
Browse files Browse the repository at this point in the history
…-crash

^ Fix crash when calling SwiftUI observable on non-main thread.
  • Loading branch information
boscojwho authored Dec 1, 2023
2 parents 6a18590 + ae244ad commit 797e9b9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Chinotto.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 14.0;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -588,7 +588,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 14.0;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand Down
7 changes: 4 additions & 3 deletions Chinotto/StorageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ final class StorageViewModel: Identifiable {
private(set) var fileSizeMetadata: [SizeMetadata] = []

/// - Parameter initial: If `true`, only calculates size if not yet calculated.
func calculateSize(initial: Bool, recalculate: Bool, shallowMetadata: Bool = false) {
@MainActor
func calculateSize(initial: Bool, recalculate: Bool, shallowMetadata: Bool = false) async {
defer { performedInitialLoad = true }

if initial, performedInitialLoad == true {
Expand Down Expand Up @@ -149,7 +150,7 @@ final class StorageViewModel: Identifiable {
)
}
#else
self.dirSize = URL.directorySize(
self.dirSize = await URL.directorySize(
url: .init(filePath: directory.userPath, directoryHint: .isDirectory)
)
#endif
Expand Down Expand Up @@ -226,7 +227,7 @@ struct StorageView: View {

private func reload() {
Task(priority: .userInitiated) {
viewModel.calculateSize(initial: false, recalculate: true)
await viewModel.calculateSize(initial: false, recalculate: true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Chinotto/UnifiedStorageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct UnifiedStorageView: View {
await withTaskGroup(of: Void.self, returning: Void.self) { taskGroup in
viewModels.forEach { viewModel in
taskGroup.addTask(priority: .userInitiated) {
viewModel.calculateSize(initial: false, recalculate: true)
await viewModel.calculateSize(initial: false, recalculate: true)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Packages/FileSystem/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let package = Package(
name: "FileSystem",
platforms: [.macOS(.v14)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
Expand Down
9 changes: 9 additions & 0 deletions Packages/FileSystem/Sources/FileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,13 @@ public extension URL {

return size
}

static func directorySize(url: URL) async -> Int {
await withCheckedContinuation { continuation in
Task(priority: .userInitiated) {
let dirSize = directorySize(url: url)
continuation.resume(returning: dirSize)
}
}
}
}

0 comments on commit 797e9b9

Please sign in to comment.