Last active
September 14, 2024 20:27
-
-
Save samsonjs/9a73531beb407b48a4a25e986c3dd01e to your computer and use it in GitHub Desktop.
Work-around for nonisolated PhotosPicker initializer in Xcode 16.0 RC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PhotosUI | |
import SwiftUI | |
private final class SendableWrapper<T>: @unchecked Sendable { | |
private var unsafeValue: T | |
private let lock = NSLock() | |
var value: T { | |
get { | |
lock.withLock { unsafeValue } | |
} | |
set { | |
lock.withLock { unsafeValue = newValue } | |
} | |
} | |
init(_ value: T) { | |
unsafeValue = value | |
} | |
} | |
extension PhotosPicker { | |
/// Work around an issue with @MainActor isolated view initializers and extension methods from 3rd party libs | |
/// not being compatible with PhotosPicker in Xcode 16.0 RC. | |
init( | |
videoSelection selection: Binding<PhotosPickerItem?>, | |
label: @escaping @MainActor @Sendable () -> Label | |
) { | |
// current encoding transcodes less frequently | |
self.init(selection: selection, matching: .videos, preferredItemEncoding: .current, photoLibrary: .shared()) { | |
MainActor.assumeIsolated { | |
SendableWrapper(label()) | |
}.value | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment