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
Docs for new APIs
  • Loading branch information
paulb777 committed Jul 23, 2021
commit 33de2d793a9511f0ab5a4c9c7b32584b064c09ef
30 changes: 29 additions & 1 deletion FirebaseStorageSwift/Sources/AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import FirebaseStorage
#if swift(>=5.5)
@available(iOS 15, *)
public extension StorageReference {
/// Asynchronously downloads the object at the StorageReference to a Data object in memory.
/// A Data object of the provided max size will be allocated, so ensure that the device has enough free
/// memory to complete the download. For downloading large files, write may be a better option.
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
///
/// - Parameters:
/// - size: The maximum size in bytes to download. If the download exceeds this size
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
/// the task will be cancelled and an error will be thrown.
/// - Returns: Data object.
func data(maxSize: Int64) async throws -> Data {
typealias DataContinuation = CheckedContinuation<Data, Error>
return try await withCheckedThrowingContinuation { (continuation: DataContinuation) in
Expand All @@ -26,7 +34,15 @@ import FirebaseStorage
}
}
}

/// Asynchronously uploads data to the currently specified StorageReference.
/// This is not recommended for large files, and one should instead upload a file from disk.
/// in the Firebase Console if desired.
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
///
/// - Parameters:
/// - uploadData: The Data to upload.
/// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
/// about the object being uploaded.
/// - Returns: StorageMetadata containing additional information about the object being uploaded.
func putDataAsync(_ uploadData: Data,
metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
typealias MetadataContinuation = CheckedContinuation<StorageMetadata, Error>
Expand All @@ -38,6 +54,13 @@ import FirebaseStorage
}
}

/// Asynchronously uploads a file to the currently specified StorageReference.
///
/// - Parameters:
/// - url: A URL representing the system file path of the object to be uploaded.
/// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
/// about the object being uploaded.
/// - Returns: StorageMetadata containing additional information about the object being uploaded.
func putFileAsync(from url: URL,
metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
typealias MetadataContinuation = CheckedContinuation<StorageMetadata, Error>
Expand All @@ -49,6 +72,11 @@ import FirebaseStorage
}
}

/// Asynchronously downloads the object at the current path to a specified system filepath.
///
/// - Parameters:
/// - fileUrl: A URL representing the system file path of the object to be uploaded.
/// - Returns: URL pointing to the file path of the downloaded file.
func writeAsync(toFile fileURL: URL) async throws -> URL {
typealias URLContinuation = CheckedContinuation<URL, Error>
return try await withCheckedThrowingContinuation { (continuation: URLContinuation) in
Expand Down