-
-
Notifications
You must be signed in to change notification settings - Fork 559
Open
Description
I am trying to implement an image decoder that needs to call out an async context, is there a better way to achieve this instead of using Semaphores?
// Example
public struct CustomImageDecoder: ImageDecoding {
func decode(_ data: Data) throws -> ImageContainer {
let sem = DispatchSemaphore(value: 0)
nonisolated(unsafe) var data = data
nonisolated(unsafe) var error: Error?
CustomImageDecoderImplementation.decode(
data: data,
completion: { result in
switch result {
case .success(let success):
data = success
case .failure(let failure):
error = failure
}
sem.signal()
}
)
switch sem.wait(timeout: .now() + 30) {
case .timedOut:
return data
case .success:
break
}
guard error == nil else {
throw error
}
return data
}
}Metadata
Metadata
Assignees
Labels
No labels