Swift 为什么firebase存储会覆盖应该在每次上传照片时保留所有用户图像的存储?

Swift 为什么firebase存储会覆盖应该在每次上传照片时保留所有用户图像的存储?,swift,firebase-realtime-database,firebase-authentication,Swift,Firebase Realtime Database,Firebase Authentication,我很确定错误在设定线上。我想,key应该让文字变得动态,但每次都会覆盖子photopost。为了存储和覆盖每次上载,我需要更改什么 class post{ var imageDownloadURL: String? var image: UIImage! var caption: String! init(image: UIImage, caption: String) { self.image = image self.c

我很确定错误在设定线上。我想,key应该让文字变得动态,但每次都会覆盖子photopost。为了存储和覆盖每次上载,我需要更改什么

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)

由于您只有一个
newPostKey
,因此一次只能有一个值。因此,在这两行中,您最终引用了云存储中的同一位置:

let newImageRef = imageStorageRef.child(newPostKey)
let newImageRef1 = imageStorageRef.child(newPostKey)
由于
newImageRef
newImageRef1
指向同一位置,最后一次写入将覆盖该位置上已经存在的内容

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

例如:

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

由于您只有一个
newPostKey
,因此一次只能有一个值。因此,在这两行中,您最终引用了云存储中的同一位置:

let newImageRef = imageStorageRef.child(newPostKey)
let newImageRef1 = imageStorageRef.child(newPostKey)
由于
newImageRef
newImageRef1
指向同一位置,最后一次写入将覆盖该位置上已经存在的内容

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

例如:

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

谢谢,这是有道理的,因为存储中有两个不同的条目,但是当新用户发布时,每个条目不是都会被覆盖吗?因为newPostRef.key=newPostKey?是的。如果希望每篇文章都是唯一的,请使用
childByAutoId
生成一个新的唯一位置。所以:
让newPostRef=Database.Database().reference().child(“people”).child(uid).child(“PhotoPosts”).childByAutoId()
啊,谢谢。我现在的问题是,在获取用于显示的图像时,快照与作为路径一部分的autoID不一致。我在问题中添加了显示和获取图像的部分。你知道如何改变这一点吗?对不起,这越来越难帮助了。我强烈建议,这样我们就可以处理一个最小的、定义明确的目标。虽然这个问题对你来说可能还是一样的,但现在对我来说已经改变了两次,这使得我很难有效地帮助你。好吧,对不起。我会看一看,尽量让它更容易理解。谢谢,这是有意义的,这样在存储中有两个不同的条目,但是当一个新用户使用newPostRef.key=newPostKey发布时,不是每个条目都会被覆盖吗?是的。如果希望每篇文章都是唯一的,请使用
childByAutoId
生成一个新的唯一位置。所以:
让newPostRef=Database.Database().reference().child(“people”).child(uid).child(“PhotoPosts”).childByAutoId()
啊,谢谢。我现在的问题是,在获取用于显示的图像时,快照与作为路径一部分的autoID不一致。我在问题中添加了显示和获取图像的部分。你知道如何改变这一点吗?对不起,这越来越难帮助了。我强烈建议,这样我们就可以处理一个最小的、定义明确的目标。虽然这个问题对你来说可能还是一样的,但现在对我来说已经改变了两次,这使得我很难有效地帮助你。好吧,对不起。我会看一看,尽量让它更容易理解。