【发布时间】:2016-11-04 06:25:19
【问题描述】:
我正在从 Firebase 存储中下载图像,如下所示:
let storage = FIRStorage.storage()
// Create a storage reference from our storage service
let storageRef = storage.reference(forURL: "MY_STORAGE_URL")
let imageRef = storageRef.child("Path_to_image")
// Download image in memory
let downloadTask = imageRef.data(withMaxSize: 1 * 1024 * 1024) {
(data, error) -> Void in
if (error != nil) {
//Handle the error
} else {
guard let imageData = data else {
print("Unable to unwrap image data.")
return
}
let downloadedImage = UIImage(data: imageData)
//Do some stuff with the image
}
}
我还使用以下观察者监视下载发生的情况:
// Observe changes in status
downloadTask.observe(.resume) { (snapshot) -> Void in
// Download resumed, also fires when the download starts
}
downloadTask.observe(.pause) { (snapshot) -> Void in
// Download paused
}
downloadTask.observe(.progress) { (snapshot) -> Void in
// Download reported progress
}
downloadTask.observe(.success) { (snapshot) -> Void in
// Download completed successfully
}
downloadTask.observe(.failure) { (snapshot) -> Void in
//Download failed
}
这一切在应用程序首次启动时运行良好。但是,如果应用程序进入后台并且我使用其他一些应用程序(Facebook、Twitter 等),然后将应用程序带回前台,我会遇到问题。如果我让应用程序打开并在前台运行超过或等于 1 小时,我也会遇到问题。
问题是let downloadTask = imageRef.data(withMaxSize: blah blah blah(在上面的第一块代码中)中的完成处理程序永远不会被调用。如果从不调用完成处理程序,我将永远无法解包数据并尝试在我的应用程序中使用图像。
此外,在downloadTask 观察者中,唯一被触发的完成处理程序是.resume 和.progress。 .success 或 .failure 事件永远不会被触发。这对我来说似乎是一个 Firebase 存储错误,但我不确定。有没有其他人遇到过类似的问题?我不明白为什么代码在新启动时就可以正常工作,但是在前台一段时间后或在后台一段时间后,图像下载停止工作。提前感谢您提供的任何意见。
【问题讨论】:
-
我没有看到
enque()方法被触发。 -
我的示例几乎直接来自文档。他们不在那里调用
enqueue()方法。你能解释一下为什么这是必要的吗? -
如果你想在iOS中后台运行,你不需要做一些特殊的事情吗? developer.apple.com/library/content/documentation/iPhone/…
标签: ios swift firebase firebase-storage