0

我正在开发与“喜欢/不喜欢帖子”类似的功能。

我有一个 MVVM 架构:

struct MyStructModel {
    var isLiked: Bool? = false
}

class MyStructView {
    var isLiked: Bool

    init(myStructModel: MyStructModel) {
        self.isLiked = myStructModel.isLiked ?? false
    }
}

我在这里成功获得了帖子是否被点赞的值;

    func isPostLiked(documentID: String, completion: @escaping (Bool) -> Void) {
    guard let authID = auth.id else { return }
    let query = reference(to: .users).document(authID).collection("liked").document(documentID)
    query.getDocument { (snapshot, error) in
        if error != nil {
            print(error as Any)
            return
        }
        guard let data = snapshot?.data() else { return }
        if let value = data["isLiked"] as? Bool {
            completion(value)
        } else {
            completion(false)
        }
    }
}

func retrieveReviews(completion: @escaping([MyStructModel]) -> ()) {
    var posts = [MyStructModel]()
    let query = reference(to: .posts).order(by: "createdAt", descending: true)
    query.getDocuments { (snapshot, error) in
        if error != nil {
            print(error as Any)
            return
        }
        guard let snapshotDocuments = snapshot?.documents else { return }
        for document in snapshotDocuments {
            if var post = try? JSONDecoder().decodeQuery(MyStructModel.self, fromJSONObject: document.decode()) {
                // isLiked is nil here...
                self.isPostLiked(documentID: post.documentID!) { (isLiked) in
                    post.isLiked = isLiked
                    print("MODEL SAYS: \(post.isLiked!)")
                    // isLiked is correct value here...
                }
                posts.append(post)
            }
            completion(posts)
        }
    }
}

但是,当它到达我的单元格时,该值仍然为零。

添加单元格代码:

var post: MyStructView? {
        didSet {
            guard let post = post else { return }
            print(post.isLiked!)
        }
    }
4

1 回答 1

0

isLiked的单元格中的属性可能为零,因为该函数在完成自身之前retrieveReviews不会等待该函数完成。isPostLiked

您可以使用DispatchGroups轻松解决此问题。这将允许您确保所有帖子isLiked在插入数组之前都正确设置了它们的值,然后只需使用 DispatchGroup 的notify块通过完成处理程序返回所有加载的帖子:

func retrieveReviews(completion: @escaping([MyStructModel]) -> ()) {
    var posts = [MyStructModel]()
    let query = reference(to: .posts).order(by: "createdAt", descending: true)
    query.getDocuments { [weak self] (snapshot, error) in
        guard let self = self else { return }
        if error != nil {
            return
        }
        guard let documents = snapshot?.documents else { return }
        let dispatchGroup = DispatchGroup()
        for document in documents {
            dispatchGroup.enter()
            if var post = try? JSONDecoder().decodeQuery(MyStructModel.self, fromJSONObject: document.decode()) {
                self.isPostLiked(documentID: post.documentID!) { isLiked in
                    post.isLiked = isLiked
                    posts.append(post)
                    dispatchGroup.leave()
                }
            }
        }
        dispatchGroup.notify(queue: .main) {
            completion(posts)
        }
    }
}
于 2020-06-05T08:42:35.453 回答