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
此功能