Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage async await #8289

Merged
merged 18 commits into from
Jul 23, 2021
Prev Previous commit
Next Next commit
Use continuations
  • Loading branch information
paulb777 committed Jul 23, 2021
commit b5fea43ec0d68cf77f93d14f014b87205e60d6a8
10 changes: 0 additions & 10 deletions FirebaseStorage/Sources/FIRStorageReference.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ - (FIRStorageUploadTask *)putData:(NSData *)uploadData
return [self putData:uploadData metadata:metadata completion:nil];
}

- (void)__putData:(NSData *)uploadData
metadata:(nullable FIRStorageMetadata *)metadata
completion:(nullable FIRStorageVoidMetadataError)completion {
[self putData:uploadData metadata:metadata completion:completion];
}

- (FIRStorageUploadTask *)putData:(NSData *)uploadData
metadata:(nullable FIRStorageMetadata *)metadata
completion:(nullable FIRStorageVoidMetadataError)completion {
Expand Down Expand Up @@ -321,10 +315,6 @@ - (FIRStorageDownloadTask *)dataWithMaxSize:(int64_t)size
return task;
}

- (void)__dataWithMaxSize:(int64_t)size completion:(FIRStorageVoidDataError)completion {
[self dataWithMaxSize:size completion:completion];
}

- (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL {
return [self writeToFile:fileURL completion:nil];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,14 @@ NS_SWIFT_NAME(putData(_:metadata:));
* or an error on failure.
* @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
*/
// clang-format off
- (FIRStorageUploadTask *)putData:(NSData *)uploadData
metadata:(nullable FIRStorageMetadata *)metadata
completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
NSError *_Nullable error))completion
NS_SWIFT_NAME(putData(_:metadata:completion:));
NS_SWIFT_NAME(putData(_:metadata:completion:));
// clang-format on

- (void)__putData:(NSData *)uploadData
metadata:(nullable FIRStorageMetadata *)metadata
completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
NSError *_Nullable error))completion
NS_SWIFT_NAME(__putDataGlue(_:metadata:completion:));
/**
* Asynchronously uploads a file to the currently specified FIRStorageReference,
* without additional metadata.
Expand Down Expand Up @@ -187,15 +184,12 @@ NS_SWIFT_NAME(putData(_:metadata:));
* or an error on failure.
* @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
*/

// clang-format off
- (FIRStorageDownloadTask *)dataWithMaxSize:(int64_t)size
completion:(void (^)(NSData *_Nullable data,
NSError *_Nullable error))completion
NS_SWIFT_NAME(getData(maxSize:completion:));

- (void)__dataWithMaxSize:(int64_t)size
completion:(void (^)(NSData *_Nullable data, NSError *_Nullable error))completion
NS_SWIFT_NAME(data(maxSize:completion:));
NS_SWIFT_NAME(getData(maxSize:completion:));
// clang-format on

/**
* Asynchronously retrieves a long lived download URL with a revokable token.
Expand Down
35 changes: 30 additions & 5 deletions FirebaseStorageSwift/Sources/AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,38 @@
import FirebaseStorage

#if swift(>=5.5)
@available(iOS 15, *)
public extension StorageReference {
@discardableResult

func data(maxSize: Int64) async throws -> Data {
typealias DataContinuation = CheckedContinuation<Data, Error>
return try await withCheckedThrowingContinuation({ (continuation: DataContinuation) in
// TODO: Use task to handle progress and cancellation.
let _ = self.getData(maxSize: maxSize) { result in
switch result {
case let .success(data):
continuation.resume(returning: data)
case let .failure(error):
continuation.resume(throwing: error)
}
}
})
}

func putDataAwait(_ uploadData: Data,
metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
// TODO: Add a parameter to capture StorageUploadTask and to enable Progress tracking.
// -> StorageUploadTask {
return try await __putDataGlue(uploadData, metadata: metadata)
metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
typealias MetadataContinuation = CheckedContinuation<StorageMetadata, Error>
return try await withCheckedThrowingContinuation({ (continuation: MetadataContinuation) in
// TODO: Use task to handle progress and cancellation.
let _ = self.putData(uploadData, metadata: metadata) { result in
switch result {
case let .success(data):
continuation.resume(returning: data)
case let .failure(error):
continuation.resume(throwing: error)
}
}
})
}
}
#endif
10 changes: 5 additions & 5 deletions FirebaseStorageSwift/Sources/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

import FirebaseStorage

private enum DataError: Error {
case internalInconsistency // Thrown when both value and error are nil.
}

/// Generates a closure that returns a `Result` type from a closure that returns an optional type
/// and `Error`.
///
Expand All @@ -31,11 +35,7 @@ private func getResultCallback<T>(completion: @escaping (Result<T, Error>) -> Vo
} else if let error = error {
completion(.failure(error))
} else {
completion(.failure(NSError(domain: "FirebaseStorageSwift",
code: -1,
userInfo: [NSLocalizedDescriptionKey:
"InternalError - Return type and Error code both nil in " +
"Storage Result generator"])))
completion(.failure(DataError.internalInconsistency))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import FirebaseStorageSwift
import XCTest

#if swift(>=5.5)
@available(iOS 15, *)
class StorageAsyncAwait: StorageIntegrationCommon {
func testGetMetadata() async throws {
let ref = storage.reference().child("ios/public/1mb2")
Expand All @@ -45,7 +46,7 @@ import XCTest
func testDelete() async throws {
let ref = storage.reference(withPath: "ios/public/fileToDelete")
let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
let result = try await ref.putDataAwait(data, metadata: nil)
let result = try await ref.putDataAwait(data)
XCTAssertNotNil(result)
_ = try await ref.delete()
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down