如何观察Firebase存储上传事件 [英] How to observe Firebase Storage Upload event

查看:112
本文介绍了如何观察Firebase存储上传事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,可以将照片上传到Firebase Storage,还有一个网络应用程序,可以连接到相同的Firebase.有没有办法从网上观察存储的变化?上传照片时,只有iOS设备本身可以访问UploadTask,并且在on事件观察器="nofollow">存储的文档就像数据库一样.

I have an iOS app which uploads photos to Firebase Storage, and a web app which is connected to the same Firebase. Is there a way to observe changes in Storage from the web? Only the iOS device itself has access to the UploadTask when a photo is being uploaded, and I did not see an on event observer in the docs for Storage like there is for Database.

推荐答案

Firebase Storage不提供与实时数据库相同的同步"功能(想象一下尝试同步1GB对象的感觉...).您将要使用数据库来同步文件上传中的元数据(例如下载URL等),如下所示:

Firebase Storage doesn't provide the same "sync" functionality as the Realtime Database (imagine what it would be like trying to sync 1GB objects...). You'll want to use the database to synchronize the metadata (such as download URLs, etc.) on file uploads like so:

// Upload file to Firebase Storage
storageRef.putData(imageData).observeStatus(.Success) { (snapshot) in
  // When the image has successfully uploaded, get it's download URL
  let url = snapshot.metadata?.downloadURL()?.absoluteString

  // Write data to the realtime database
  dbRef.child("photos").childByAutoId().setValue(["name": snapshot.metadata?.name, "url": url])
}

...

dbRef.child("photos").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in 
  // snapshot contains the name and URL of the uploaded file
});

代码来自

Code is from Zero to App, a talk we did at I/O 2016, which details this a little better

这篇关于如何观察Firebase存储上传事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆