使用 Swift 从 firebase 获取数据时,集合视图滚动不流畅?
Collection view not scroll smoothly when fetch data from firebase using Swift?
我正在使用 JSQMessage 库和 Firebase 后端服务制作聊天应用程序。当用户发送大量媒体消息(如图像或视频)时,我遇到了问题。我也使用 SDWebImage 库,但它不能解决问题。当我 运行 我的应用程序在模拟器中时,集合视图滚动流畅,但当我在真实设备中尝试时,它很糟糕。所以请帮我解决这个问题。感谢您的贡献。
这是我获取数据的代码
databaseReference.child("Message").child(fromId).child(toId).queryLimited(toLast: UInt(numberMess)).observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String : Any] {
let receiverId = dict["ReceiverId"] as? String ?? ""
let senderId = dict["SenderId"] as? String ?? ""
let time = dict["Time"] as? NSNumber ?? 0
let mediatype = dict["MediaType"] as? String ?? ""
let status = dict["Status"] as? String ?? ""
let sendername = dict["SenderName"] as? String ?? ""
let receivername = dict["ReceiverName"] as? String ?? ""
if mediatype == "Text" {
let text = dict["Text"] as! String
let newJSQMess = JSQMessage(senderId: senderId, displayName: sendername, text: text)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: sendername, ReceiverNameString: receivername, ReceiverIdString: receiverId, TimeNumber: time)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
} else {
newMess.ChatId = newMess.SenderId
}
newMess.Status = status
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMess!)
self.ClassMessages.append(newMess)
DispatchQueue.main.async {
self.finishReceivingMessage()
self.collectionView.reloadData()
}
}
}
else if mediatype == "Photo" {
let photo = JSQPhotoMediaItem(image: nil)
let PhotoUrl = dict["PhotoUrl"] as? String ?? ""
let downloader = SDWebImageDownloader.shared()
downloader.downloadImage(with: URL(string: PhotoUrl), options: [], progress: nil, completed: { (image, data, error, finished) in
DispatchQueue.main.async(execute: {
photo?.image = image
self.collectionView.reloadData()
})
})
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: photo!, Status: status, PhotoUrlString: PhotoUrl, ThumnailUrlString: "", VideoUrlString: "", StatusString: status)
}
else if mediatype == "Video" {
let thumnailImage = dict["ThumnailImageUrl"] as! String
let thumnailImageUrl = URL(string: thumnailImage)
let data = try? Data(contentsOf: thumnailImageUrl!)
let videoUrl = dict["VideoUrl"] as? String ?? ""
let video = URL(string: videoUrl)
let videoItem = JSQVideoMediaItem(fileURL: video!, isReadyToPlay: true, thumbnailImage: UIImage.sd_image(with: data!))
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: videoItem, Status: status, PhotoUrlString: "", ThumnailUrlString: thumnailImage, VideoUrlString: videoUrl, StatusString: status)
}
}
})
// show media in chat log
private func showMediaData(senderId: String, displayName: String, text: String, ReceiverId: String, Receivername : String, TimeNumber: NSNumber, media: JSQMediaItem, Status : String, PhotoUrlString : String, ThumnailUrlString : String, VideoUrlString : String, StatusString : String) {
let newJSQMedia = JSQMessage(senderId: senderId, displayName: displayName, media: media)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: displayName, ReceiverNameString: Receivername, ReceiverIdString: ReceiverId, TimeNumber: TimeNumber)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
media.appliesMediaViewMaskAsOutgoing = true
} else {
newMess.ChatId = newMess.SenderId
media.appliesMediaViewMaskAsOutgoing = false
}
newMess.PhotoUrl = PhotoUrlString
newMess.Status = StatusString
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMedia!)
self.ClassMessages.append(newMess)
self.finishReceivingMessage()
DispatchQueue.main.async() {
self.collectionView.reloadData()
}
}
}
我找到了我的错误的解决方案。似乎 collectionView 重新加载很多时间取决于发送消息的数量。所以解决方案很简单,只需删除 collectionView 重新加载数据
我正在使用 JSQMessage 库和 Firebase 后端服务制作聊天应用程序。当用户发送大量媒体消息(如图像或视频)时,我遇到了问题。我也使用 SDWebImage 库,但它不能解决问题。当我 运行 我的应用程序在模拟器中时,集合视图滚动流畅,但当我在真实设备中尝试时,它很糟糕。所以请帮我解决这个问题。感谢您的贡献。
这是我获取数据的代码
databaseReference.child("Message").child(fromId).child(toId).queryLimited(toLast: UInt(numberMess)).observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String : Any] {
let receiverId = dict["ReceiverId"] as? String ?? ""
let senderId = dict["SenderId"] as? String ?? ""
let time = dict["Time"] as? NSNumber ?? 0
let mediatype = dict["MediaType"] as? String ?? ""
let status = dict["Status"] as? String ?? ""
let sendername = dict["SenderName"] as? String ?? ""
let receivername = dict["ReceiverName"] as? String ?? ""
if mediatype == "Text" {
let text = dict["Text"] as! String
let newJSQMess = JSQMessage(senderId: senderId, displayName: sendername, text: text)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: sendername, ReceiverNameString: receivername, ReceiverIdString: receiverId, TimeNumber: time)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
} else {
newMess.ChatId = newMess.SenderId
}
newMess.Status = status
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMess!)
self.ClassMessages.append(newMess)
DispatchQueue.main.async {
self.finishReceivingMessage()
self.collectionView.reloadData()
}
}
}
else if mediatype == "Photo" {
let photo = JSQPhotoMediaItem(image: nil)
let PhotoUrl = dict["PhotoUrl"] as? String ?? ""
let downloader = SDWebImageDownloader.shared()
downloader.downloadImage(with: URL(string: PhotoUrl), options: [], progress: nil, completed: { (image, data, error, finished) in
DispatchQueue.main.async(execute: {
photo?.image = image
self.collectionView.reloadData()
})
})
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: photo!, Status: status, PhotoUrlString: PhotoUrl, ThumnailUrlString: "", VideoUrlString: "", StatusString: status)
}
else if mediatype == "Video" {
let thumnailImage = dict["ThumnailImageUrl"] as! String
let thumnailImageUrl = URL(string: thumnailImage)
let data = try? Data(contentsOf: thumnailImageUrl!)
let videoUrl = dict["VideoUrl"] as? String ?? ""
let video = URL(string: videoUrl)
let videoItem = JSQVideoMediaItem(fileURL: video!, isReadyToPlay: true, thumbnailImage: UIImage.sd_image(with: data!))
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: videoItem, Status: status, PhotoUrlString: "", ThumnailUrlString: thumnailImage, VideoUrlString: videoUrl, StatusString: status)
}
}
})
// show media in chat log
private func showMediaData(senderId: String, displayName: String, text: String, ReceiverId: String, Receivername : String, TimeNumber: NSNumber, media: JSQMediaItem, Status : String, PhotoUrlString : String, ThumnailUrlString : String, VideoUrlString : String, StatusString : String) {
let newJSQMedia = JSQMessage(senderId: senderId, displayName: displayName, media: media)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: displayName, ReceiverNameString: Receivername, ReceiverIdString: ReceiverId, TimeNumber: TimeNumber)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
media.appliesMediaViewMaskAsOutgoing = true
} else {
newMess.ChatId = newMess.SenderId
media.appliesMediaViewMaskAsOutgoing = false
}
newMess.PhotoUrl = PhotoUrlString
newMess.Status = StatusString
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMedia!)
self.ClassMessages.append(newMess)
self.finishReceivingMessage()
DispatchQueue.main.async() {
self.collectionView.reloadData()
}
}
}
我找到了我的错误的解决方案。似乎 collectionView 重新加载很多时间取决于发送消息的数量。所以解决方案很简单,只需删除 collectionView 重新加载数据