Skip to content

[FR]: Add cancellation support to Storage async/await APIs #11786

Open
@fthdgn

Description

@fthdgn

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions