应用程序在后台运行时,Firestore存储上传任务观察器未执行

时间:2019-01-06 20:16:40

标签: ios swift firebase google-cloud-firestore

我正在将2个文件上传到存储设备,一旦完成两个操作,便应创建一个Firestore文档。如果应用程序在前台,则效果很好。

但是,如果仍在上传时将应用置于后台,则不会执行.success观察者代码,.progress观察者仍将执行直到100%,然后一切都停止。

一旦我将应用放回前台,代码就会再次执行(有时成功,有时会出现错误,告诉我检查服务器响应)。

调用上传功能的按钮:

@IBAction func postButtonClicked(_ sender: Any) {
        print("Post button clicked, trying to post...")
        //Make sure we are logged in and have a video
        guard let videoURL = videoURL else {
            print("We don't have a video, why are we trying to post?")
            dismiss(animated: true, completion: nil)
            return
        }

        //1 Create and start uploading thumbnail
        createThumbnail(from: videoURL) { (image) in
            if let image = image {
                self.uploadThumbnailToStorage(image: image)
            }
        }
        //2 Start uploading video
        uploadVideoToStorage(from: videoURL)

        dismiss(animated: true, completion: nil)
    }

以及视频上传功能:

func uploadVideoToStorage(from url: URL) {
        print("Start uploading video...")
        let storageRef = storage.reference()
        let videoRef = storageRef.child("videos/\(postRef.documentID)_video")

        // Upload the file to the path "videos"
        let uploadTask = videoRef.putFile(from: url, metadata: nil) { metadata, error in
            guard let metadata = metadata else {
                // Uh-oh, an error occurred!
                return
            }
            // Metadata contains file metadata such as size, content-type.
            let size = metadata.size
            print("Video file size: \(size)")

        }

        //Observe uploadTask
        uploadTask.observe(.success) { snapshot in
            snapshot.reference.downloadURL { (url, error) in
                guard let downloadURL = url else {
                    if let error = error {
                        print("Error in video success observer: \(error.localizedDescription)")
                    }
                    return
                }
                print("Video upload success.")
                self.videoString = downloadURL.absoluteString
                self.videoUploadComplete = true

                self.checkIfUploadsAreComplete()
            }
        }

        uploadTask.observe(.progress) { snapshot in
            let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
                / Double(snapshot.progress!.totalUnitCount)
            print("Uploading video: \(percentComplete.rounded())%")
        }

    }

checkIfUploadsAreComplete()函数检查两个上传任务是否均已完成,然后编写文档,但我认为代码无关。

我曾尝试像这样将uploadVideoToStorage()函数置于后台:

DispatchQueue.global(qos: .background).async {
            // Request the task assertion and save the ID.
            self.backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "TestVideoUpload") {
                // End the task if time expires.
                UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
                self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
            }

            // Send the data synchronously.
            DispatchQueue.main.async {
            self.uploadVideoToStorage(from: videoURL)
            }

            // End the task assertion.
            UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
            self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
        }

但是那没做任何事情,有什么想法吗?

0 个答案:

没有答案