i tried to set an image from Firebase to ImageView using SDWebImage, i get the url of firebase, but when i try to set the image i get the error in DispatchQueue. I download the url and get the url of firebase without problem to set it in a imageView but only when i use DispatchQueue crash the app. i tried commenting DispatchQueue but i need the main thread.
// ConversationsTableView+Delegates.swift
import Foundation
import UIKit
extension ConversationsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return conversations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = conversations[indexPath.row]
guard let cell = tableView.dequeueReusableCell(withIdentifier: ConversationsTableViewCell.id, for: indexPath) as?
ConversationsTableViewCell else {
fatalError("Could not cast ConversationsTableViewCell")
}
cell.configure(with: model)
cell.accessoryType = .disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let model = conversations[indexPath.row]
let vc = ChatViewController(with: model.otherUserEmail)
vc.title = model.name
vc.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
}
// ConversationsTableViewCell.swift
import UIKit
import SDWebImage
class ConversationsTableViewCell: UITableViewCell {
static let id = "ConversationsTableViewCellId"
private var userImageView: UIImageView = UIImageView()
private var userNameLabel: UILabel = UILabel()
private var userMessageLabel: UILabel = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .white
setUpView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setUpView() {
// Configuración de elementos UI
userImageView.contentMode = .scaleAspectFill
userImageView.layer.cornerRadius = 50
userImageView.layer.masksToBounds = true
userImageView.layer.cornerRadius = 30
userImageView.clipsToBounds = true
userImageView.backgroundColor = .blue
userNameLabel.font = .systemFont(ofSize: 21, weight: .semibold)
userMessageLabel.font = .systemFont(ofSize: 19, weight: .regular)
userMessageLabel.numberOfLines = 0
[userImageView, userNameLabel, userMessageLabel].forEach {
contentView.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
// constraints
NSLayoutConstraint.activate([
userImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10),
userImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
userImageView.widthAnchor.constraint(equalToConstant: 60),
userImageView.heightAnchor.constraint(equalToConstant: 60),
userNameLabel.centerYAnchor.constraint(equalTo: userImageView.centerYAnchor, constant: 9),
userNameLabel.leadingAnchor.constraint(equalTo: userImageView.trailingAnchor),
userNameLabel.widthAnchor.constraint(equalToConstant: 100),
userNameLabel.heightAnchor.constraint(equalToConstant: 40),
userMessageLabel.bottomAnchor.constraint(equalTo: userNameLabel.bottomAnchor, constant: 5),
// userMessageLabel.centerYAnchor.constraint(equalTo: userImageView.centerYAnchor, constant: 9)
])
}
public func configure(with model: Conversation) {
self.userMessageLabel.text = model.latestMessage.message
self.userNameLabel.text = model.name
let path = "images/\(model.otherUserEmail)_profile_picture.png"
StorageManager.shared.downloadURL(for: path, completion: { [weak self] result in
switch result {
case .success(let url):
print(url)
DispatchQueue.main.sync { //Error occurred here
self?.userImageView.sd_setImage(with: url)
}
print("Reload.")
case .failure(let error):
print("Failed to get image url: \(error)")
}
})
}
}