【问题标题】:Having errors after updating Firebase pods. <downloadURL> <storageRef>更新 Firebase pod 后出现错误。 <downloadURL> <storageRef>
【发布时间】:2018-12-21 00:13:27
【问题描述】:

升级了我的 Firebase pod,但出现了大量错误。我使用了 Firebase Docs 网页上的 Docs,所以我能够修复其中的大部分。任何帮助深表感谢。谢谢

错误:

  1. “StorageMetadata”类型的值没有成员“downloadURL”

  2. “StorageReference”类型的值没有成员“put”

完整代码:

import Foundation
import FirebaseStorage
class HelperService {
    static func uploadDataToServer(data: Data, videoUrl: URL? = nil, ratio: CGFloat, caption: String, onSuccess: @escaping () -> Void) {
        if let videoUrl = videoUrl {
            self.uploadVideoToFirebaseStorage(videoUrl: videoUrl, onSuccess: { (videoUrl) in
                uploadImageToFirebaseStorage(data: data, onSuccess: { (thumbnailImageUrl) in
                    sendDataToDatabase(photoUrl: thumbnailImageUrl, videoUrl: videoUrl, ratio: ratio, caption: caption, onSuccess: onSuccess)
                })
            })
            //self.senddatatodatabase
        } else {
            uploadImageToFirebaseStorage(data: data) { (photoUrl) in
                self.sendDataToDatabase(photoUrl: photoUrl, ratio: ratio, caption: caption, onSuccess: onSuccess)
            }
        }
    }

    static func uploadVideoToFirebaseStorage(videoUrl: URL, onSuccess: @escaping (_ videoUrl: String) -> Void) {
        let videoIdString = NSUUID().uuidString
        let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("posts").child(videoIdString)
        storageRef.putFile(from: videoUrl, metadata: nil) { (metadata, error) in
            if error != nil {
                ProgressHUD.showError(error!.localizedDescription)
                return
            }

错误 1. > if let videoUrl = metadata?.downloadURL()?.absoluteString {

                onSuccess(videoUrl)
            }
        }
    }

    static func uploadImageToFirebaseStorage(data: Data, onSuccess: @escaping (_ imageUrl: String) -> Void) {
        let photoIdString = NSUUID().uuidString
        let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("posts").child(photoIdString)

错误 2. > storageRef.put(data, metadata: nil) { (metadata, error) in

            if error != nil {
                ProgressHUD.showError(error!.localizedDescription)
                return
            }
            if let photoUrl = metadata?.downloadURL()?.absoluteString {
                onSuccess(photoUrl)
            }

        }
    }

    static func sendDataToDatabase(photoUrl: String, videoUrl: String? = nil, ratio: CGFloat, caption: String, onSuccess: @escaping () -> Void) {
        let newPostId = Api.Post.REF_POSTS.childByAutoId().key
        let newPostReference = Api.Post.REF_POSTS.child(newPostId)

        guard let currentUser = Api.User.CURRENT_USER else {
            return
        }

        let words = caption.components(separatedBy: CharacterSet.whitespacesAndNewlines)
        for var word in words {
            if word.hasPrefix("#") {
                word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)
                word = word.trimmingCharacters(in: CharacterSet.symbols)
                let newHashReference = Api.HashTag.REF_HASHTAG.child(word.lowercased())
                newHashReference.setValue([newPostId: true])
//                let hashTagsRef = DataService.dataService.BASE_REF.child("hashTags").child(postKey)
//                let data = ["to": "", "by": "\(DataService.dataService.currentUserId!)", "hashTag": word.lowercased(), "comment": self.captionTextView.text] as [String : Any]
//                hashTagsRef.setValue(data)
            }
        }

        let currentUserId = currentUser.id
        var dict = ["id": currentUserId! ,"photoUrl": photoUrl, "caption": caption, "likeCount": 0, "ratio": ratio] as [String : Any]
        if let videoUrl = videoUrl {
            dict["videoUrl"] = videoUrl
        }
        newPostReference.setValue(dict, withCompletionBlock: {
            (error, ref) in
            if error != nil {
                ProgressHUD.showError(error!.localizedDescription)
                return
            }

            Api.Feed.REF_FEED.child(Api.User.CURRENT_USER!.id!).child(newPostId).setValue(true)

            let myPostRef = Api.MyPosts.REF_MYPOSTS.child(currentUserId!).child(newPostId)
            myPostRef.setValue(true, withCompletionBlock: { (error, ref) in
                if error != nil {
                    ProgressHUD.showError(error!.localizedDescription)
                    return
                }
            })
            ProgressHUD.showSuccess("Success")
            onSuccess()
        })
    }
}

【问题讨论】:

    标签: ios swift firebase firebase-storage


    【解决方案1】:

    删除了从上传元数据中获取下载 URL 的功能。相反,您可以使用StorageReference.downloadURL()

    要上传文件,您有一些选项,如API docs here 所示。你可以看到putDataputFile有变种,但put没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-14
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多