Open
Description
Description
There are many functions on FirebaseStorage with callback parameter for async operations. These functions return an object to cancel the operations. Such as StorageReference.getData(maxSize: Int64, completion: @escaping (Result<Data, Error>) -> Void) -> StorageDownloadTask
.
These functions also has a wrapper function which support Swfit Concurrency.
However, these wrapper functions simply use withCheckedThrowingContinuation
without any cancellation feature. It would be better that these functions also utilize withTaskCancellationHandler
feature.
class Holder<T> {
var value: T?
}
public extension StorageReference {
// This is the current Firebase implementation
func dataNotCancellable(maxSize: Int64) async throws -> Data {
return try await withCheckedThrowingContinuation { continuation in
_ = self.getData(maxSize: maxSize) { result in
continuation.resume(with: result)
}
}
}
// This is the proposed implementation
func dataCancallable(maxSize: Int64) async throws -> Data {
let holder: Holder<StorageDownloadTask> = .init()
return try await withTaskCancellationHandler(operation: {
try await withCheckedThrowingContinuation { continuation in
holder.value = self.getData(maxSize: maxSize) { result in
continuation.resume(with: result)
}
}
}, onCancel: {
holder.value?.cancel()
})
}
}
API Proposal
No response
Firebase Product(s)
Storage