2017-04-13 288 views
1

从存储下载时,我想设置一个较小的超时时间,例如,只有5 - 10秒,这可能吗?iOS:Firebase存储设置超时时间

我downloadiung这样的:

 let storage = FIRStorage.storage() 
     let storageRef = storage.reference(forURL: "gs://...") 
     let fileRef = storageRef.child(myLink) 
     let downloadTask = fileRef.write(toFile: url) { url, error in 
     ... 

回答

0

我会做一个延伸到StorageDownloadTask类,并添加设置与发射是否抵消请求的请求时间的计时器功能。

事情是这样的:

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 
} 
0

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

此功能