【问题标题】:Why is firebase Storage is overwriting the storage that should keep all user images each time a photo is uploaded?为什么每次上传照片时,firebase 存储都会覆盖应保留所有用户图像的存储?
【发布时间】:2020-05-20 21:01:19
【问题描述】:

我很确定错误在于设置行。我认为 .key 应该使写作动态化,但子照片每次都会被覆盖。为了存储和覆盖每次上传,我需要进行哪些更改。

class post{
    var imageDownloadURL: String?

    var image: UIImage!

    var caption: String!


    init(image: UIImage, caption: String) {
        self.image = image
        self.caption = caption
    }
    func save() {

        let uid = Auth.auth().currentUser!.uid
        let newPostRef = Database.database().reference().child("people").child(uid).child("PhotoPosts")
        let newPostRef1 = Database.database().reference().child("people").child(uid).child("PhotoPosts1")


        let newPostKey = newPostRef.key


        if let imageData = image.jpegData(compressionQuality: 0.6){

            let imageStorageRef = Storage.storage().reference().child("images")


            let newImageRef = imageStorageRef.child(newPostKey)
            let newImageRef1 = imageStorageRef.child(newPostKey)


            newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
                self.imageDownloadURL =
                    snapshot.metadata?.downloadURL()?.absoluteString
                newPostRef.setValue(self.imageDownloadURL as Any)


            })



            newImageRef1.putData(imageData).observe(.success, handler: {(snapshot) in
                self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString

                let keyToPost = Database.database().reference().child("people").child(uid).childByAutoId().key
                let f1: [String: Any] = [(keyToPost) : self.imageDownloadURL as Any]

                newPostRef1.updateChildValues(f1)


                               })


        }

    }


}

这里是数据库规则:

    rules_version = '2';
  service firebase.storage {
   match /b/{bucket}/o {
     match /{allPaths=**} {
       allow read, write: if request.auth != null;
     }
  }
 }

注意:我知道 newimageref1 指的是与 newimageref 相同的东西,但我不明白这在逻辑上是如何导致覆盖的。我很确定这不是导致覆盖的原因。

正确答案后更新:

正在获取图像。

 public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

    let immy = cell.viewWithTag(1) as! UIImageView



        let person: Userx = people[indexPath.row]


        cell.postID = self.people[indexPath.row].postID


        if let PhotoPosts = person.PhotoPosts {
            let url = URL(string: PhotoPosts)
            immy.sd_setImage(with: url)

        }


        return cell

........

for people in snapshot.children.allObjects as! [DataSnapshot] {

                    if people.key != thisUsersUid {
                        print("peoplekey",people.key)
                       let peoplePhotoPosts = peopleObject?["PhotoPosts"]  as? String
                            let peopl = Userx(PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, ...)
                    self.people.append(peopl)





                    self.people.append(peopl)

【问题讨论】:

    标签: swift firebase-realtime-database firebase-authentication


    【解决方案1】:

    由于您只有一个newPostKey,它一次只能有一个值。因此,在这两行中,您最终会引用 Cloud Storage 中的相同位置:

    let newImageRef = imageStorageRef.child(newPostKey)
    let newImageRef1 = imageStorageRef.child(newPostKey)
    

    由于 newImageRefnewImageRef1 指向同一个位置,最后一次写入会覆盖该位置已经存在的任何内容。

    我认为您希望根据最低级别的数据库键写入两个不同的位置,在这种情况下,您需要确保根据各个键设置存储引用。

    例如:

    let newImageRef = imageStorageRef.child(newPostRef.key)
    let newImageRef1 = imageStorageRef.child(newPostRef1.key)
    

    【讨论】:

    • 谢谢,这是有道理的,因为存储中有 2 个不同的条目,但是当新用户发布时,每个条目都不会被覆盖,因为 newPostRef.key = newPostKey?
    • 是的。如果您希望每个帖子都是唯一的,请使用childByAutoId 生成一个新的唯一位置。所以:let newPostRef = Database.database().reference().child("people").child(uid).child("PhotoPosts").childByAutoId()
    • 啊,谢谢。我现在的问题是,在获取图像时,显示快照与作为路径的一部分的 autoID 不一致。我在问题中添加了显示和获取图像的部分。你知道如何改变它吗?
    • 抱歉,这个问题越来越难解决了。我强烈推荐creating a minimal, complete, verifiable example,以便我们处理一个最小的、明确定义的目标。虽然问题对您来说可能仍然存在,但现在对我来说已经转移了两次,这使得很难有效地提供帮助。
    • 好的,抱歉。我会看一下并尝试使其更易于理解。
    猜你喜欢
    • 2020-12-22
    • 2019-04-18
    • 2019-08-08
    • 2021-11-21
    • 2021-02-10
    • 2020-10-05
    • 2014-08-02
    • 2021-11-20
    • 2016-11-18
    相关资源
    最近更新 更多