离开视图控制器时,Firebase Storage Upload Task进度观察器不可见



因此,在用户将视频上传到Firebase的视图控制器上,我有一个UIProgressViewUILabel来观察上传任务的进度。这非常好用问题是当我离开视图控制器时UIProgressView被重置,我不知道如何重新获得这些信息来继续观察进展,因为这都是异步的一旦用户离开视图控制器,是否有任何方法可以阻止视图控制器重新加载,从而使进度视图和标签保持不变而不重新加载这里有一些代码供参考(负责观察任务的功能(:

func uploadVideo(videoURL: URL)
{
let storage = Storage.storage()
let storageRef = storage.reference()

let videoRef = storageRef.child("rPosts/(uid!)/(fileID).mov")
let metadata = StorageMetadata()
metadata.contentType = "video/quicktime"

var videoData: Data = Data()

do
{
videoData = try Data(contentsOf: videoURL)
}
catch
{
print(error.localizedDescription)
return
}

let taskReference = videoRef.putData(videoData, metadata: metadata)
{ (metaData, error) in
guard error == nil else
{
self.errorLabel.text = error!.localizedDescription
return
}
}

taskReference.observe(.progress)
{ [weak self] (snapshot) in
guard let progress = snapshot.progress?.fractionCompleted else { return }
self?.progressView.progress = Float(progress)
self?.progressLabel.text = "(round(100 * Float(progress)))% (2/2)"

if progress == 1
{
self!.progressLabel.text = "Upload Successful!"
self!.progressLabel.textColor = .black
self!.progressView.progress = 0
}
}
}

如何访问taskReference

您只需要一种更好的方法来处理这一问题。你不应该把这个上传相关的东西放在你的视图控制器里。

你可以将上传的相关内容移动到一些单独的文件中,如下所示:

protocol StorageManagerObserver: AnyObject {
func storageManager(_ storageManager: StorageManager, didProgressUpload fractionCompleted: Float)
func storageManager(_ storageManager: StorageManager, didFailWithError error: Error)
}

class StorageManager: NSObject  {

static var shared = StorageManager()
private var observers: [StorageManagerObserver] = []

let storage = Storage.storage()

deinit {
observers.removeAll()
}

func addObserver(_ observer: StorageManagerObserver) {
if observers.contains(where: { $0 === observer }) == false {
observers.append(observer)
}
}

func removeObserver(_ observer: StorageManagerObserver) {
guard let index = self.observers.firstIndex(where: { $0 === observer }) else {
return
}
self.observers.remove(at: index)
}


func uploadVideo(videoURL: URL, uid: Int?, fileID: Int) {
let storageRef = storage.reference()

let videoRef = storageRef.child("rPosts/(uid!)/(fileID).mov")
let metadata = StorageMetadata()
metadata.contentType = "video/quicktime"

var videoData: Data = Data()

do {
videoData = try Data(contentsOf: videoURL)
}
catch {
print(error.localizedDescription)
return
}

let taskReference = videoRef.putData(videoData, metadata: metadata) { [weak self] (metaData, error) in
guard let self = self else { return }
guard let someError = error else { return }
for anObserver in self.observers {
anObserver.storageManager(self, didFailWithError: someError)
}
}

taskReference.observe(.progress) { [weak self] (snapshot) in
guard let self = self else { return }
guard let progress = snapshot.progress?.fractionCompleted else { return }
for anObserver in observers {
anObserver.storageManager(self, didProgressUpload: progress)
}
}
}
}

我已经制作了这个StorageManager单例,您可以根据需要使用其他方法(因为我们需要保持它的活力,直到它完成(。现在,您可以将其用于任何视图控制器,如以下所示:

class YourViewController: UIViewController {

@IBOutlet weak var errorLabel: UILabel!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()

StorageManager.shared.addObserver(self)
}

deinit {
StorageManager.shared.removeObserver(self)
}
}
extension YourViewController: StorageManagerObserver {
func storageManager(_ storageManager: StorageManager, didProgressUpload fractionCompleted: Float) {
self.progressView.progress = fractionCompleted
self.progressLabel.text = "(round(100 * fractionCompleted))% (2/2)"

if fractionCompleted == 1 {
self.progressLabel.text = "Upload Successful!"
self.progressLabel.textColor = .black
self.progressView.progress = 0
}
}

func storageManager(_ storageManager: StorageManager, didFailWithError error: Error) {
self.errorLabel.text = error.localizedDescription
}
}

相关内容

  • 没有找到相关文章

最新更新