- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathAsyncAwait.swift
91 lines (86 loc) · 4.25 KB
/
AsyncAwait.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import FirebaseStorage
#if swift(>=5.5)
@available(iOS 15,*)
publicextensionStorageReference{
/// 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.
///
/// - Parameters:
/// - size: The maximum size in bytes to download. If the download exceeds this size
/// the task will be cancelled and an error will be thrown.
/// - Returns: Data object.
func data(maxSize:Int64)asyncthrows->Data{
typealiasDataContinuation=CheckedContinuation<Data,Error>
returntryawaitwithCheckedThrowingContinuation{(continuation:DataContinuation)in
// TODO: Use task to handle progress and cancellation.
_ =self.getData(maxSize: maxSize){ result in
continuation.resume(with: result)
}
}
}
/// 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.
///
/// - 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)asyncthrows->StorageMetadata{
typealiasMetadataContinuation=CheckedContinuation<StorageMetadata,Error>
returntryawaitwithCheckedThrowingContinuation{(continuation:MetadataContinuation)in
// TODO: Use task to handle progress and cancellation.
_ =self.putData(uploadData, metadata: metadata){ result in
continuation.resume(with: result)
}
}
}
/// 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)asyncthrows->StorageMetadata{
typealiasMetadataContinuation=CheckedContinuation<StorageMetadata,Error>
returntryawaitwithCheckedThrowingContinuation{(continuation:MetadataContinuation)in
// TODO: Use task to handle progress and cancellation.
_ =self.putFile(from: url, metadata: metadata){ result in
continuation.resume(with: result)
}
}
}
/// 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)asyncthrows->URL{
typealiasURLContinuation=CheckedContinuation<URL,Error>
returntryawaitwithCheckedThrowingContinuation{(continuation:URLContinuation)in
// TODO: Use task to handle progress and cancellation.
_ =self.write(toFile: fileURL){ result in
continuation.resume(with: result)
}
}
}
}
#endif