Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Firebase-跨多个位置的原子写入不工作_Ios_Swift_Firebase_Swift3_Firebase Realtime Database - Fatal编程技术网

Ios Firebase-跨多个位置的原子写入不工作

Ios Firebase-跨多个位置的原子写入不工作,ios,swift,firebase,swift3,firebase-realtime-database,Ios,Swift,Firebase,Swift3,Firebase Realtime Database,如何使用此函数更新Firebase中的实时数据库?我设法使函数工作,但只在触发updateChild时为帖子写入新ID。我怎样才能通过他们的帖子ID来更新当前的帖子 var pathToPictures = [Pictures]() func updateAllImagesOfCurrentUserInDatabase() { //refrences let ref = FIRDatabase.database().reference() let uid =

如何使用此函数更新Firebase中的实时数据库?我设法使函数工作,但只在触发updateChild时为帖子写入新ID。我怎样才能通过他们的帖子ID来更新当前的帖子

   var pathToPictures = [Pictures]()
   func updateAllImagesOfCurrentUserInDatabase() {

    //refrences
    let ref = FIRDatabase.database().reference()
    let uid = FIRAuth.auth()?.currentUser?.uid

    //match the user ID value stored into posts with current userID and get all the posts of the user
    let update = ref.child("posts").queryOrdered(byChild: "userID").queryEqual(toValue: uid)
    update.observe(FIRDataEventType.value, with: { (snapshot) in
           // print(snapshot)

        self.pathToPictures.removeAll()

        if snapshot.key != nil {

        let results = snapshot.value as! [String : AnyObject]

        for (_, value) in results {
            let pathToPostPicture = Pictures()

            if let pathToImage = value["pathToImage"] as? String , let postID = value["postID"] as? String {
                pathToPostPicture.postImageUrl = pathToImage
                pathToPostPicture.postID = postID
                self.pathToPictures.append(pathToPostPicture)
                print("Image and POST ID: \(pathToPostPicture.postImageUrl!)")
                print("Post ID is : \(postID)")

                if FIRAuth.auth()?.currentUser?.uid == uid {
                ref.child("Users_Details").child(uid!).child("profileImageUrl").observeSingleEvent(of: .value, with: { (userSnapshot) in
                    print(userSnapshot)

                    let userIMageUrl = userSnapshot.value as! String
                    pathToPostPicture.postImageUrl = userIMageUrl
                    self.pathToPictures.append(pathToPostPicture)
                    print("This is the image path:" + userIMageUrl + String(self.pathToPictures.count))

                    // Generate the path
                    let newPostRef = ref.child("posts").childByAutoId()
                    let newKey = newPostRef.key as String
                    print(newKey)
                    let updatedUserData = ["posts/\(postID)/pathToImage": pathToPostPicture.postImageUrl]
                    print("This is THE DATA:" , updatedUserData)
                    ref.updateChildValues(updatedUserData as Any as! [AnyHashable : Any])

                })

                }

                print(self.pathToPictures.count)

            }

            }
        } else {
            print("snapshot is nill")

        }

        self.collectionView?.reloadData()

    })
}
更新:这就是我的数据库的样子

这是我的数据库:

“用户详细信息”:{
“AR0NRARJWVOHHBFB8YUFAO64Z62”:{
“profileImageUrl”:“url”,
“用户ID”:“AR0NRARJWVOHHBFB8YUFAO64Z62”
},
“oGxXznrS2DS4ic1ejcSfKB5UlIQ2”:{
“profileImageUrl”:“url”,
“用户ID”:“oGxXznrS2DS4ic1ejcSfKB5UlIQ2”
}
},
“员额”:{
“-KlzNLcofTgqJgfTaGN9”:{
“全名”:“全名”,
“间隔”:1.496785712879506E9,
“正常日期”:“2017年6月6日星期二22:48”,
“路径图像”:“url”,
“用户ID”:“oGxXznrS2DS4ic1ejcSfKB5UlIQ2”
},
“-KlzNXfvecIwBatXxmGW”:{
“全名”:“全名”,
“间隔”:1.496785761349721E9,
“正常日期”:“2017年6月6日星期二22:49”,
“路径图像”:“url”,
“用户ID”:“oGxXznrS2DS4ic1ejcSfKB5UlIQ2”

},
看起来您将下载大量重复数据。虽然数据库的非规范化是一种良好的做法,但我认为,在这种情况下,您最好不要在每篇文章中都包含相同的下载URL。这对少数文章没有影响,但如果您有数千名用户和数十名或更多用户成千上万的帖子被删除,这意味着大量的额外数据被下载。相反,你可以拥有一个包含uid作为键和profile imageUrl作为值的字典。你可以检查字典中所需的uid,如果它不存在,则查询数据库中该用户的
用户详细信息
,然后将它们添加到字典中。When你需要显示图像,你可以从这本词典中获得url。其他人可能对此有更好的建议,所以我欢迎其他想法

如果您希望在每篇文章中保留配置文件图像,那么我建议使用Firebase的云函数。您可以创建一个函数,当用户的
user\u Details
中更新
profileImageUrl
时,在其他文章中更新此条目。目前,云函数仅在Node.js中可用。如果您不知道JS,请不要让它成为一种威慑!学习一点JS绝对是值得的。这些示例向您展示了入门所需的JS。 查看以下资源:


在与逻辑斗争了几天之后,我终于得到了答案。如果有人会遇到类似的情况,这就是答案。在我的例子中,这是关于当用户更改其图像时更新当前用户的所有帖子(我的帖子使用非规范化,因此每个图像路径都不同)。它将循环浏览帖子,找到与其用户ID匹配的当前用户postsID,将它们放入数组中,找到当前用户图片并使用图像路径更新该数组。最后将更新Firebase数据库

var pathToPictures = [Pictures]()
func updateAllImagesOfCurrentUserInDatabase() {

    //refrences
    let ref = FIRDatabase.database().reference()
    let uid = FIRAuth.auth()?.currentUser?.uid

    //match the user ID value stored into posts with current userID and get all the posts of the user
    let update = ref.child("posts").queryOrdered(byChild: "userID").queryEqual(toValue: uid)
    update.observe(FIRDataEventType.value, with: { (snapshot) in
        self.pathToPictures.removeAll()

        if snapshot.value as? [String : AnyObject] != nil {
        let results = snapshot.value as! [String : AnyObject]

        for (_, value) in results {
            let pathToPostPicture = Pictures()

            if let pathToImage = value["pathToImage"] as? String , let postID = value["postID"] as? String {
                pathToPostPicture.postImageUrl = pathToImage
                pathToPostPicture.postID = postID
                self.pathToPictures.append(pathToPostPicture)
                print("Image and POST ID: \(pathToPostPicture.postImageUrl!)")
                print("Post ID is : \(postID)")

                if FIRAuth.auth()?.currentUser?.uid == uid {
                ref.child("Users_Details").child(uid!).child("profileImageUrl").observeSingleEvent(of: .value, with: { (userSnapshot) in
                    print(userSnapshot)

                    let userIMageUrl = userSnapshot.value as! String
                    pathToPostPicture.postImageUrl = userIMageUrl
                    self.pathToPictures.append(pathToPostPicture)
                    print("This is the image path:" + userIMageUrl + String(self.pathToPictures.count))

                    // Update the Database

                    let postIDCount = pathToPostPicture.postID!
                    let updatedUserData = ["posts/\(postIDCount)/pathToImage": pathToPostPicture.postImageUrl!]
                    print("This is THE DATA:" , updatedUserData)
                    ref.updateChildValues(updatedUserData as Any as! [AnyHashable : Any])

                })
                }
                print(self.pathToPictures.count)
            }
            }
        } else {
            print("snapshot is nill - add some data")
        }

        self.collectionView?.reloadData()
    })
}

如果我理解你的代码的功能,你希望它在更新用户的个人资料图片时更新他们所有帖子中的用户个人资料图片的所有实例吗?另外,你能提供一个数据库结构图吗?@JenPerson是的,这正是我想要实现的。我已经用数据库更新了我的问题。我无法理解我想用这个更新数据库。好的,我想我有一个建议,但再次澄清:你用相同的图片更新该用户的每一篇文章,这是用户的个人资料图片,对吗?是的,这是正确的!你有什么想法?@JenPersonI感谢你的回答,但这并不是让我离开t他目前正在考虑这种情况。做了一些研究后,我发现我需要进行原子更新!我用我认为对我有用的代码更新了我的问题。你能看一下并帮助一个新手在这方面使用有效的代码吗?正如我所见,Firebase是你的障碍,所以我推荐你比我聪明。:)