0

I am using JSQMessageViewController for chat implementation. i am able to send and receive messages through json Post and get.

i am able to load my chat history without any error. used timer(5 sec) for retrieving the messages.

It works fine for some time,, it reloads the messages for each 5 sec.

after a while i am getting "fatal error: Index out of range" suddenly on below line.

override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData {

     //app Crashes here : fatal error: Index out of range  
         return messages[indexPath.item]


}

BELOW IS CODE var messages = JSQMessage

var outgoingBubbleImageView:JSQMessagesBubbleImage! //= JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleGreen())
var incomingBubbleImageView:JSQMessagesBubbleImage! //= JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray())


 var batchMessages = true

// Refresh Messages
var timer : Timer!

// Don't double load!
var isLoading = false

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
    collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

    outgoingBubbleImageView = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: uicolorFromHex(0xDCF8C7))
    incomingBubbleImageView = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray()) //uicolorFromHex(0xF6F6F6)


    let defaults = UserDefaults.standard
    let usertype = defaults.object(forKey: "UserType") as! String

    self.userType = usertype

// self.getChatHistory(ipAddress+"/VIOService.svc/JSONService/chatorderhistoryget/"+EmailId+"/"+toEmail+"/"+OrderId) // self.finishReceivingMessage()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(addTapped))
    self.sender = userType

    self.inputToolbar.contentView?.leftBarButtonItem = nil


    // This is a beta feature that mostly works but to make things more stable it is diabled.
    collectionView?.collectionViewLayout.springinessEnabled = false

    automaticallyScrollsToMostRecentMessage = true

   // self.collectionView?.reloadData()
   self.collectionView?.layoutIfNeeded()




   self.isLoading = false

  self.loadMessages()


}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    collectionView!.collectionViewLayout.springinessEnabled = false


 timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ChatMessagesViewController.loadMessages), userInfo: nil, repeats: true)


}

func addMessage(_ id: String, text: String) {
    print("add message: \(text)")



    let message = JSQMessage(senderId: id, displayName: "Me", text: text)


    messages.append(message)



}

func loadMessages() {

// self.messages = [JSQMessage]()

  //  self.messages.removeAll()

  if self.isLoading == false {

        isLoading = true

        self.getChatHistory(mYURL)


        finishReceivingMessage()


    }

}

func getChatHistory(_ url:String) {

messages.removeAll()
  //  messages.removeAll(keepingCapacity: true)

    let requestURL: URL = URL(string: url)!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {data,response,error in

        let httpResponse = response as! HTTPURLResponse
        let statusCode = httpResponse.statusCode

        if (statusCode == 200) {
            print("everything is fine, file downloaded successfully.")

            print(response!)


            do{



                let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)

                if let items = json as? [[String: AnyObject]] {



                    for item in items {

                        if let sentBy = item["SentBy"] as? String {

                            if let SentTo = item["SentTo"] as? String {

                                if let Message = item["Msg"] as? String {

                                    if let IsType = item["IsType"] as? String  {

                                        if let mesSentOn = item["MsgSenton"] as? String //Date {

                                        {

//
let add = chatHistory(SentBy: sentBy, SentTo: SentTo, Message: Message, IsType: IsType, SentOn: mesSentOn)

                                            self.chatHist.append(add)

                                            self.from = SentTo
                                            self.to = sentBy

                                            var message:String=""

                                            if let range = Message.range(of: ";") {

                                             message = Message.substring(from: range.upperBound)
                                            }

                                           self.addMessage(IsType, text: message)


                                        }
                                    }
                                }
                            }

                        }

                     self.do_collectionView_refresh()

                    }



                }



            }
            catch {

                print("Error with Json: \(error)")
            }

        }
    }


    isLoading = false

    task.resume()



}

func do_collectionView_refresh() {

    DispatchQueue.main.async {

        self.collectionView?.reloadData()

    }
}


override func senderId() -> String {
    return userType
}

override func senderDisplayName() -> String {
    return "Me"
}

override func collectionView(_ collectionView: UICollectionView,
                             numberOfItemsInSection section: Int) -> Int {

    if messages.count > 0 {

        return messages.count
    }
    else {

        return 0
    }


  //  return messages.count
}


override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData {

     //app Crashes here : fatal error: Index out of range  
         return messages[indexPath.item]


}


override func collectionView(_ collectionView: JSQMessagesCollectionView, messageBubbleImageDataForItemAt indexPath: IndexPath) -> JSQMessageBubbleImageDataSource {

//    return messages[indexPath.item].senderId == self.senderId() ? outgoingBubble : incomingBubble

    let message = messages[indexPath.item]

    //   let message = chatHist[indexPath.item]
    // 1
    if message.senderId == userType { //

        return outgoingBubbleImageView
    } else { // 3
        return incomingBubbleImageView
    }

}

i am stuck with the error, please someone help.

8
  • @DilipTiwari Yes i resolved that issue..Let me know what you are looking for.
    – JB29.4
    Jul 25, 2018 at 10:00
  • could u help me i need to show json response in chat page bro @JB29.4 Help me plz Jul 25, 2018 at 10:02
  • Yeah sure, I am still using timer to load my messages.
    – JB29.4
    Jul 25, 2018 at 10:04
  • can u share ur code jsoneditoronline.org/?id=7730bbdde4e44a41a302647d3192bfa6 this is my json can u create a private chat if possible for u @JB29.4 Jul 25, 2018 at 10:04
  • sure buddy, I am little occupied now...will look into your JSON
    – JB29.4
    Jul 25, 2018 at 10:09

1 Answer 1

0

Let me know if this makes a difference:

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

}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {

    let data = self.messages[indexPath.row]
    return data

}

 override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]
    switch(data.senderId) {
    case self.senderId:
        return self.outgoingBubbleImageView
    default:
        return self.incomingBubbleImageView
    }
}
1
  • Thanks for the reply @Alex7. i tried above solution but it didn't work for me.
    – JB29.4
    Nov 10, 2017 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.