gpt4 book ai didi

ios - 已发送的消息在 JSQMessageViewController 中添加了两次

转载 作者:可可西里 更新时间:2023-10-31 23:44:21 25 4
gpt4 key购买 nike

我正在使用 JSQMessageviewcontroller 通过解析创建一个聊天客户端。除了“我发送的消息在 jsqmessage 数组中添加了两次”之外,我成功地实现了所有目标。 Althuogh 我已经检查过 JSQMessage 数组是否已经包含该消息,不应添加它,但在发送消息的情况下条件为 false。

这是我的代码:

import UIKit
import JSQMessagesViewController
import Parse

class MessageViewController: JSQMessagesViewController {

var chatWith : String?
var chattingWith = false
let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor(red: 10/255, green: 180/255, blue: 230/255, alpha: 1.0))
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
var messages = [JSQMessage]()
var updateTimer = NSTimer()
let updateDelay = 2.0
var jsqMessages : [JSQMessage] = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setup()

updateTimer = NSTimer.scheduledTimerWithTimeInterval(updateDelay, target: self, selector: "update", userInfo: nil, repeats: true)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidDisappear(animated: Bool) {

updateTimer.invalidate()

}
func reloadMessagesView() {
self.collectionView?.reloadData()
}
}

//MARK - Setup
extension MessageViewController {


func setup() {
self.senderId = PFUser.currentUser()?.objectId!
self.senderDisplayName = PFUser.currentUser()?.username!

}
}

//MARK - Data Source
extension MessageViewController {

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}

override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
self.messages.removeAtIndex(indexPath.row)
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let data = messages[indexPath.row]
switch(data.senderId) {
case self.senderId:
return self.outgoingBubble
default:
return self.incomingBubble
}
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
}

//MARK - Toolbar
extension MessageViewController {
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
let message = JSQMessage(senderId: PFUser.currentUser()?.objectId!, senderDisplayName: PFUser.currentUser()?.username!, date: date, text: text)
print("sent messages = \(message)")
if !self.messages.contains(message) {
self.messages += [message]
}

self.sendMessageToParse(message , date: date)
self.finishSendingMessage()
}

override func didPressAccessoryButton(sender: UIButton!) {

}


}

//MARK - Syncano
extension MessageViewController {

func sendMessageToParse(message: JSQMessage , date : NSDate) {

let obj = PFObject(className: "Chat")
obj.setObject((PFUser.currentUser()?.username)!, forKey: "username")
obj.setObject(message.text!, forKey: "Text")
obj.setObject(chatWith!, forKey: "To")
obj.setObject(PFUser.currentUser()!, forKey: "user")
obj.setObject(date, forKey: "dateCreatedOn")
try! obj.save()


}

func update() {
let query = PFQuery(className: "Chat")
query.limit = 10
query.includeKey("user")
// query.whereKey("To", equalTo: chatWith!)
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in

for i in results! {

if i.objectForKey("username") as? String == PFUser.currentUser()?.username! {

let message = self.jsqMessageFromParseMessage(i, senderId: (PFUser.currentUser()?.objectId!)!, senderDisplayName: i.objectForKey("username")! as! String)
print("received message : \(message)")
self.jsqMessageArray(message)
}else {

let message = self.jsqMessageFromParseMessage(i, senderId: i.objectForKey("To")! as! String, senderDisplayName: i.objectForKey("To")! as! String)
self.jsqMessageArray(message)
}

}


}

}



func jsqMessageFromParseMessage(message: PFObject , senderId : String , senderDisplayName : String) -> JSQMessage {
let jsqMessage = JSQMessage(senderId: senderId, senderDisplayName: senderDisplayName, date: message["dateCreatedOn"] as! NSDate, text: message["Text"] as! String)
return jsqMessage
}

func jsqMessageArray(jsqmessage : JSQMessage){

if self.messages.contains(jsqmessage) {

}else {
//
self.messages.append(jsqmessage)
self.finishReceivingMessage()
self.reloadMessagesView()

}

}
}

如果有人能提供帮助,将不胜感激。提前致谢。

最佳答案

有一个方法可以在 JSQMessageData 协议(protocol)上实现,叫做 messageHash。它返回一个 Int 并且它是每条消息的唯一 ID。您应该能够使用它来检查消息是否相同。

您需要创建一个遵守 JSQMessageData 协议(protocol)的对象数据类,而不是使用 JSQMessage

  class Message: JSQMessageData {

func senderId()-> String {
return //the result of currentUser()?.objectId
}

func senderDisplayName()-> String {
return //the result of currentUser()?.username!
}

func date()-> NSDate {
return // date
}

func isMediaMessage() -> Bool {
return false
}

func messageHash()-> String {
return //return a uniquely created id
}

func text()-> String {
return // text
}

}

您应该可以在 PFUser 上执行此操作,尽管我对 Parse 不够熟悉,无法告诉您这是否可能,但您实际上只是将现有属性重新映射为新名称,所以不应该一个问题

关于ios - 已发送的消息在 JSQMessageViewController 中添加了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34448685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com