Firebase Storage
有一个功能,您可以pause()
,cancel()
和resume()
阅读here
我会成立一个类属性为StorageUploadTask
然后我会暂停或ca NCEL在DispatchAsync Timer
使用DispatchWorkItem
:
// 1. make sure you `import Firebase` to have access to the `StorageUploadTask`
import Firebase
var timeoutTask: DispatchWorkItem?
var downloadTask: StorageUploadTask?
// using your code from your question
let storage = FIRStorage.storage()
let storageRef = storage.reference(forURL: "gs://...")
let fileRef = storageRef.child(myLink)
// 2. this cancels the downloadTask
timeoutTask = DispatchWorkItem{ [weak self] in
self?.downloadTask?.cancel()
}
// 3. this executes the code to run the timeoutTask in 5 secs from now. You can cancel this from executing by using timeoutTask?.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: timeoutTask!)
// 4. this initializes the downloadTask with the fileRef your sending the data to
downloadTask = fileRef.write(toFile: url) {
(url, error) in
self.timeoutTask?.cancel()
// assuming it makes it to this point within the 5 secs you would want to stop the timer from executing the timeoutTask
self.timeoutTask = nil // set to nil since it's not being used
}
不幸的是火力地堡没有可用于RealTimeDatabase
此功能