应用程序在后台花了一些时间后,Firebase存储下载任务未完成

时间:2016-11-04 06:25:19

标签: ios swift firebase firebase-storage

我正在从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存储漏洞,但我不确定。有没有其他人遇到过类似的问题?我不明白为什么代码在新的发布时会正常工作,但是经过一段时间在前台或者在后台运行一段时间后,图像下载就会停止工作。提前感谢您提供的任何意见。

1 个答案:

答案 0 :(得分:0)

不幸的是,这是目前预期的行为。 Firebase存储(目前)仅限前景:如果应用程序背景化,我们没有持久保存上传网址,并且无法在后台上传,也不能在它退出后台后重新启动,因此可能会被操作系统和项目未上传。

这是我们想要解决的下一件大事(our Android SDK makes it possible,虽然并不容易)但不幸的是,目前我们还没有取得更多进展。

作为一个旁注,你的观察者在活动改变后不会存在 - 一旦应用程序背景化,downloadTask就消失了,所以当它回到前台时,我们基本上需要一个方法它检索当前背景的所有任务,并允许您挂钩观察者。类似的东西:

FIRStorage.storage().backgroundedTasks { (tasks) -> Void in
  // tasks is an array of upload and download tasks
  // not sure if it needs to be async
}