firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

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

Open fthdgn opened this issue 2 years ago • 3 comments

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

fthdgn avatar Sep 07 '23 21:09 fthdgn

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Sep 07 '23 21:09 google-oss-bot

@fthdgn Thanks! At first glance, this looks like a great suggestion to me. We'll take a deeper look and likely add to an upcoming release. In the meantime, we'd be happy to review a PR that flushes it out further.

paulb777 avatar Sep 07 '23 21:09 paulb777

Any additional suggestions about how to use and test the cancellation feature on the Swift Concurrency variation of the Storage APIs?

How does cancellation get exposed to the API client? Without including the StorageDownloadTask in the API it's not clear how cancellation can ever happen.

paulb777 avatar Jan 02 '24 18:01 paulb777