成功获取,但价值仍然为零。社交媒体类型提要

Success in fetching but value is still nil. Social media type feed

提问人:robowarrior 提问时间:11/3/2023 最后编辑:robowarrior 更新时间:11/3/2023 访问量:49

问:

我正在尝试构建一个社交媒体类型的提要,该提要获取数组中的第一条评论并将其附加到帖子中供其他人查看。

目前,注释有一个空的用户变量,我需要通过另一个函数手动获取它。

以下代码成功获取用户并成功附加帖子。firstcomment,但是 post.firstcomment 用户是 nil?

这是我的代码

       func fetchAllPosts(isRefreshing: Bool = false) {
    FirebaseService.shared.fetchInitialFeed(document: document) { documents, error in
        guard let documents = documents else {
            print(error?.localizedDescription ?? "Error")
            return
        }

        var list: [PostModel] = []
        let dispatchGroup = DispatchGroup()

        for doc in documents {
            var post = PostModel(doc.data())

            if post.isCommentPresent {
                for commentId in post.commentsList {
                    dispatchGroup.enter()

                    FirebaseService.shared.fetchCommentPreview(postId: post.uid, commentId: commentId) { document, error in
                        defer {
                            dispatchGroup.leave()
                        }

                        guard let doc = document else {
                            print(error?.localizedDescription ?? "")
                            return
                        }

                        guard let data = doc.data() else {
                            return
                        }

                        var comment = CommentModel(data)

                        // Fetch the comment owner
                        dispatchGroup.enter()
                        FirebaseService.shared.fetchOwner(uid: comment.createdBy) { ownerDocument, ownerError in
                            defer {
                                dispatchGroup.leave()
                            }

                            guard let ownerDocument = ownerDocument else {
                                print(ownerError?.localizedDescription ?? "")
                                return
                            }

                            if let ownerData = ownerDocument.data() {
                                let commentOwner = UserModel(ownerData)
                                comment.user = commentOwner
                            }
                        }
                        
                        // Assign the updated comment to the post within this completion block
                        post.firstComment = comment
                    }
                }
            }

            dispatchGroup.enter()

            FirebaseService.shared.fetchOwner(uid: post.createdBy) { document, error in
                guard let document = document else {
                    print(error?.localizedDescription ?? "")
                    dispatchGroup.leave()
                    return
                }

                if let data = document.data() {
                    let postOwner = UserModel(data)
                    post.postOwner = postOwner
                    if selection.capitalized == "Explore" {
                        list.append(post)
                    } else if selection.capitalized == "Connections" {
                        if post.createdBy.isInMyFriendsList || post.createdBy.isEqualTomyUid {
                            list.append(post)
                            print(post.firstComment)
                        }
                    }
                }

                dispatchGroup.leave()
            }
        }

        dispatchGroup.notify(queue: .main) {
            document = documents.last

            if documents.isEmpty {
                isLastDoc = true
            }

            withAnimation {
                if isRefreshing {
                    postList = list.sorted(by: { $0.time.timeIntervalSince1970 > $1.time.timeIntervalSince1970 })
                } else {
                    postList += list.sorted(by: { $0.time.timeIntervalSince1970 > $1.time.timeIntervalSince1970 })
                }
                isLoading = false
                isFinished = true
            }
        }
    }
}

顺便说一句,任何可以提出更好的方法来做这个系统的人都会加分,我有一种感觉,它可能无法扩展。

Swift Firebase 谷歌云 Firestore

评论

0赞 lorem ipsum 11/3/2023
stackoverflow.com/questions/73636543/......
0赞 robowarrior 11/3/2023
@JoakimDanielson帖子已更新
0赞 robowarrior 11/3/2023
我是@JoakimDanielson?

答: 暂无答案