我对完成处理程序很新,这就是为什么我要问这个问题。 我需要有一个工作完成处理程序,以便图像在返回之前获取firebase数据库的数据。这是代码段。
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.item]
var profileImage: UIImage!
if message.senderUID == currentUser.uid {
return nil
} else {
setAvatarImages(message.senderUID, Image: profileImage, completion: { (error) in
print("done")
})
return JSQMessagesAvatarImageFactory.avatarImage(with: profileImage, diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
}
}
func setAvatarImages(_ UID: String!, Image: UIImage!, completion: @escaping (NSError?) -> Void) {
var Image = Image
let ref = DatabaseReference.users(uid: UID).reference()
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let user = User(dictionary: snapshot.value as! [String : Any])
print("done")
user.downloadProfilePicture(completion: { (image, error) in
if image != nil {
Image = image
} else if error != nil {
print(error?.localizedDescription ?? "")
}
})
})
}
问题是,setAvatarImages函数没有执行它应该如何,而是立即调用返回并返回一个值为nil的图像,这会导致致命错误。 感谢每一位帮助!