Ios 解析Messenger聊天室:消息顺序错误
最近我的ios messenger有点问题。起初我只收到短信,一切都很顺利。当我试图从解析中获取图像时,我成功了;然而,饲料的顺序不正确。 它似乎完全忽略了“query.orderByAscending”Ios 解析Messenger聊天室:消息顺序错误,ios,swift,parse-platform,chat,messenger,Ios,Swift,Parse Platform,Chat,Messenger,最近我的ios messenger有点问题。起初我只收到短信,一切都很顺利。当我试图从解析中获取图像时,我成功了;然而,饲料的顺序不正确。 它似乎完全忽略了“query.orderByAscending” fetchMessages() { currentUser = PFUser.currentUser()! let query = PFQuery(className:"Messages") query.whereKey("convoid", equalTo:con
fetchMessages()
{
currentUser = PFUser.currentUser()!
let query = PFQuery(className:"Messages")
query.whereKey("convoid", equalTo:convoid)
query.orderByAscending("createdAt")
query.cachePolicy = .NetworkElseCache
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
dispatch_async(dispatch_get_main_queue()) {
if let objects = objects {
for object in objects {
if(object["fileType"] as? String == "photo"){
if(object["senderId"] as? String == self.currentUser.objectId!){
let userImageFile = object["file"] as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let imageddata = UIImage(data:imageData!)
let chatBubbleData = ChatBubbleData(text: "", image:imageddata, date: object.createdAt, type: .Mine)
self.addChatBubble(chatBubbleData)
self.chatBubbleDatas.append(chatBubbleData)
}
}
}else{
let userImagefile = object["file"] as! PFFile
userImagefile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let imageddata = UIImage(data:imageData!)
let chatBubbleData = ChatBubbleData(text: "", image:imageddata, date: object.createdAt, type: .Opponent)
self.addChatBubble(chatBubbleData)
self.chatBubbleDatas.append(chatBubbleData)
}
}
}
}else{
if(object["senderId"] as? String == self.currentUser.objectId!){
let chatBubbleData = ChatBubbleData(text: object["text"] as? String, image:nil, date: object.createdAt, type: .Mine)
self.addChatBubble(chatBubbleData)
self.chatBubbleDatas.append(chatBubbleData)
}else{
let chatBubbleData = ChatBubbleData(text: object["text"] as? String, image:nil, date: object.createdAt, type: .Opponent)
self.addChatBubble(chatBubbleData)
self.chatBubbleDatas.append(chatBubbleData)
}
}
}
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
}
self.messageCointainerScroll.contentSize = CGSizeMake(CGRectGetWidth(messageCointainerScroll.frame), lastChatBubbleY + internalPadding)
self.addKeyboardNotifications()
}
除了消息视图没有按正确的顺序显示所有消息之外,其他一切都很好。事实上,所有文本消息的顺序都是正确的,但是不管发生什么情况,不管创建日期是什么,消息图像总是在后面。我认为这与装载有关;然而,我知道斯威夫特,我不完全确定。关于修复或参考的任何见解,请分享 我的第一个猜测是,这与您在主队列上的call to do操作的位置有关:
dispatch\u async(dispatch\u get\u main\u queue()){
您可能只在最后需要更新用户界面时才想这样做。问题是,由于整个程序块都是在后台运行的,因此耗时较长的操作可能会在耗时较短的操作之后得到处理(在另一个队列上,因此您不知道主队列上的调度情况…)
进一步检查后,您的区块内似乎有一系列其他后台调用,例如:
userImagefile.getDataInBackgroundWithBlock
我已经使用Parse和JSQMessagesViewController编写了类似的代码,我认为这就是您正在做的。我强烈建议您找到通过网络将用户界面更新与模型更新分离的方法。您现在所拥有的不仅难以调试,而且可能会在某种难以检测的异步方面造成问题同步方式。它实际上是使用
query.orderByAscending(“createdAt”)
来获取对象。只需在query.findObjectsInBackgroundithBlock
中添加打印(对象)
,您就会看到所有对象都以正确的顺序打印
加载图像会导致顺序不正确。当您获得文本对象时,您会立即追加数据。但当对象是图像时,您会调用userImageFile.GetDataInBackgroundithBlock
,它会在调用追加数据的块之前下载图像
您需要做的是为图像添加一个占位符,使其位于正确的位置,然后在图像下载完成后更新它
作为旁注,您可能想研究一下,它将去掉所有的
As?
if语句,并去掉。有趣的是占位符将在哪里发挥作用?它会在之前吗?userImageFile.getDataInBackgroundWithBlock我一定会尝试使用它,谢谢!是的,userImageFile.getDataInBackgroundWithBlock
正在添加延迟,这会打乱订单。在此之前添加占位符不会有延迟。非常感谢。我会尝试查看它是否正常工作,它会将图像按正确的顺序放置,但在所有文本之后仍然显示图像感谢您的建议非常感谢!我一定会尝试使用ui通过网络更新和模型更新