Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Last active September 14, 2024 20:27
Show Gist options
  • Save samsonjs/9a73531beb407b48a4a25e986c3dd01e to your computer and use it in GitHub Desktop.
Save samsonjs/9a73531beb407b48a4a25e986c3dd01e to your computer and use it in GitHub Desktop.
Work-around for nonisolated PhotosPicker initializer in Xcode 16.0 RC
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