iOS:Firebase存储设置超时 [英] iOS: Firebase Storage set timeout
本文介绍了iOS:Firebase存储设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从存储下载时,我想设置一个较小的超时时间,例如仅5-10秒,这可能吗?
when downloading from storage, I would like to set a smaller timeout, e.g. only 5 - 10 seconds, is this possible?
我这样下载:
let storage = FIRStorage.storage()
let storageRef = storage.reference(forURL: "gs://...")
let fileRef = storageRef.child(myLink)
let downloadTask = fileRef.write(toFile: url) { url, error in
...
推荐答案
我将对StorageDownloadTask类进行扩展,并添加一个函数,该函数将计时器设置为具有所请求的时间的计时器,该计时器将在触发时取消请求.
I would make a extension to the StorageDownloadTask class and add a function that sets a timer with the requested time that cancels the request if fired.
类似这样的东西:
extension StorageDownloadTask {
func withTimout(time: Int, block: @escaping () -> Void) {
Timer.scheduledTimer(withTimeInterval: TimeInterval(time), repeats: false) { (_) in
self.cancel()
block()
}
}
所以您应该写:
fileRef.write(toFile: url, completion: {
(url, error) in
...
}).withTimeout(time: 10) {
//Here you can tell everything that needs to know if the download failed that it did
}
这篇关于iOS:Firebase存储设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文