将数据同步从具有相同上传的帖子中保留



我正在使用一个应用程序,您可以在其中发布不同类别的图片。例如:动物/摩托车。

我的用户可以关注用户并查看其图片。

现在,我正在研究以下工作:我想在类别中上传帖子,同时在所有帖子中(用户可以随机查看所有发布的图片(

每个帖子,无论在哪里发布,都将在AllPosts Child

var REF_POST = Database.database().reference().child("posts")

对于每个类别,都有一个特定的孩子,其中仅此类别的帖子留在

var REF_POST = Database.database().reference().child("motorcycles")

现在在提要中,您可以喜欢并发表评论而没有问题。它使用孩子" AllPosts"的数据

现在,我想自动将数据与摩托车中的帖子保持同步。他们都共享相同的密钥,但显然它只会在" AllPosts"孩子中进行更新。

目标是始终在" AllPosts"中始终更新每个类别并反向。因此,如果用户在类别中喜欢,它也将在" AllPosts"中更新。

更新代码

func incrementLikesFeed(postId: String,  onSuccess: @escaping (Post) -> Void, onError: @escaping (_ errorMessage: String?) -> Void){
    let postRef = API.Post.REF_POSTS.child(postId)
    postRef.runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
        if var post = currentData.value as? [String : AnyObject], let uid = API.User.CURRENT_USER?.uid {
            var likes: Dictionary<String, Bool>
            likes = post["likes"] as? [String : Bool] ?? [:]
            var likeCount = post["likeCount"] as? Int ?? 0
            var score = post["score"] as? Int ?? 0
            if let _ = likes[uid] {
                likeCount -= 1
                score -= 100
                likes.removeValue(forKey: uid)
                self.REF_LIKES_POSTS.child(postId).child(uid).removeValue()
            } else {
                likeCount += 1
                score += 100
                likes[uid] = true
                self.REF_LIKES_POSTS.child(postId).child(uid).setValue(true)
            }
            post["score"] = score as AnyObject?
            post["likeCount"] = likeCount as AnyObject?
            post["likes"] = likes as AnyObject?
            currentData.value = post
            return TransactionResult.success(withValue: currentData)
        }
        return TransactionResult.success(withValue: currentData)
    }) { (error, committed, snapshot) in
        if let error = error {
            print(error.localizedDescription)
            onError(error.localizedDescription)
        }
        if let dict = snapshot?.value as? [String: Any] {
            let post = Post.transformPost(dict: dict, key: snapshot!.key)
        onSuccess(post)
        }
    }
}

例如,此帖子在所有帖子中更新类似节点。因为我将有10个类别,所以我很想在特定数据库路径中始终更新拟合帖子

示例:

database.database().reference.child("Text").child("shortJokes")
database.database().reference.child("allPosts") -> where all posts are

我如何才能实现该特定孩子的更新?

因为以后我会像那个

database.database().reference.child("Text").child("Sport")
database.database().reference.child("Text").child("Animals")
database.database().reference.child("Pictures").child("funnyPictures")
database.database().reference.child("Pictures").child("sweetAnimals")

我希望它可以检查所有帖子的整个数据库,以查看孩子包含相同帖子的位置。当我有10个以上的类别时,我看到了问题,我必须将10个功能称为一个,可能总是会更新所有内容,而不仅仅是一个具有相同ID的帖子。

更新

            let newUserData = ["score": score, "likeCount": likeCount, "likes": likes] as [String : Any]
            let postRefEinzeiler = self.REF_EINZEILER.child(postId)
            postRefEinzeiler.updateChildValues(newUserData)
            let postRefSchwHumor = self.REF_SCHWARZERHUMOR.child(postId)
            postRefSchwHumor.updateChildValues(newUserData)

使用此代码段,我设法实现了我想做的事情。但是,它总是在其他类别中使用我提供的数据添加一个带有帖子的节点。

我如何才能首先查看帖子是否已经存在,并且只有存在以更新值而存在?

var BASE_POST = Database.database().reference()
// Generate a new push ID for the new post
let postkey = ENTER YOUR POST_ID 
let postData = YOUR_POST_DETAILS_DICTIONARY
// Create the data we want to update
let updatedPostData = ["posts/(postkey)": postData, 
              "motorcycles/(postkey)": postData ]

// Do a deep-path update
ref.updateChildValues(updatedPostData, withCompletionBlock: { (error, ref) -> Void in
    if (error) {
        print("Error updating data: (error.description)")
    }
})

最新更新