等待MessageKit函数中的另一个函数完成

时间:2019-03-04 20:30:26

标签: swift messagekit

由于在我的其他SA问题上为我提供了帮助的人,我能够创建一个函数,该函数返回一个布尔值,以查看用户是否已对聊天消息进行投票。如果该人使用MessageKit messageBottomLabelAttributedText函数对聊天消息投票了,我想打印。但是,我无法使用返回的布尔值来打印正确的文本。

这是我当前在MessagesDataSource中的messageBottomLabelAttributedText函数:

    func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

        var bool = didAlreadyVote(message: message as! MessageType){_ in Bool.self}

        if bool as? Bool == true {
            let dateString = self.formatter.string(from: message.sentDate)
            let likeString = "Voted"
            return NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
        } else {
            let dateString = self.formatter.string(from: message.sentDate)
            return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
        }
    }
}

作为参考,以下是该社区较早前帮助我的didAlreadyVote函数:

func didAlreadyVote(message: MessageType, completion: @escaping (Bool) -> Void) {

    // check user votes collection to see if current message matches
    guard let currentUser = Auth.auth().currentUser else {return}
    let userID = currentUser.uid
    let docRef = Firestore.firestore().collection("users").document(userID).collection("upvotes").whereField("messageId", isEqualTo: message.messageId)

    docRef.getDocuments { querySnapshot, error in

        if let error = error {
            print("Error getting documents: \(error)")
            completion(false)
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
                completion(true) /// Note that this will get called multiple times if you have more the one document!
            }
        }
    }
}

运行应用程序时,bool变量不返回任何内容。如何从函数中检索布尔值,然后在messageBottomLabelAttributedText中使用它?

谢谢!

1 个答案:

答案 0 :(得分:1)

别等。

didAlreadyVote返回任何内容。它包含一个异步完成处理程序,该处理程序将Bool值作为参数传递。

语法是

    didAlreadyVote(message: message){ boolValue in   
        if boolValue {
            let dateString = self.formatter.string(from: message.sentDate)
            let likeString = "Voted"
            return NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
        } else {
            let dateString = self.formatter.string(from: message.sentDate)
            return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
        }

但是您无论如何都不能在messageBottomLabelAttributedText中使用它,因为除非您还实现了一个完成处理程序,否则您将无法返回任何内容

func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath, completion: @escaping (NSAttributedString) -> Void) {
    didAlreadyVote(message: message){ boolValue in   
        if boolValue {
            let dateString = self.formatter.string(from: message.sentDate)
            let likeString = "Voted"
            completion(NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]))
        } else {
            let dateString = self.formatter.string(from: message.sentDate)
            completion(NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]))
        }
    }
}