Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when show typing indicator #1387

Closed
shahchirag2110 opened this issue Jun 23, 2020 · 3 comments
Closed

Crash when show typing indicator #1387

shahchirag2110 opened this issue Jun 23, 2020 · 3 comments
Labels

Comments

@shahchirag2110
Copy link

shahchirag2110 commented Jun 23, 2020

Describe the bug
Typing indicator work perfectly till i did not add custom cell. After adding custom cell typing indicator force crashes when setTypingIndicatorViewHidden method called

To Reproduce
Steps/code to reproduce the behavior:
Add custom cell and then it will crash the application

private func showTypingIndicator() {
        firebaseManager.observerOnUpdateOnValue(in: TagRootDirectory.Chat, withPath: "\(self.objID.actualID)/\(TagSubDirectoryChat.Detail.rawValue)") {  [weak self] (objAny) in
            guard let weakSelf = self else {
                return
            }
            if weakSelf.isFirstLoad == false {
                if let dictData = objAny as? NSDictionary,let typingStatus = dictData["typing"] as? String {
                    if typingStatus == "1" {
                        weakSelf.setTypingIndicatorViewHidden(false, animated: true, whilePerforming: {
                            
                        }) { (isCompleted) in
                            if weakSelf.isLastSectionVisible() == false{
                                weakSelf.messagesCollectionView.scrollToBottom()
                            }
                        }
                        
                    } else {
                        weakSelf.setTypingIndicatorViewHidden(false, animated: true, whilePerforming: {
                        }) { (isCompleted) in
                            if weakSelf.isLastSectionVisible() == false{
                                weakSelf.messagesCollectionView.scrollToBottom()
                            }
                        }
                    }
                    
                }
            }
        }
    }

func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        return messageList.count
    }
    func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int {
        return messageList[section].arrChat.count
    }
    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messageList[indexPath.section].arrChat[indexPath.row] //Crash here
    }
    
    func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
        let objMessageData = messageList[indexPath.section]
        
        return NSAttributedString(string: objMessageData.strDate ?? "", attributes: [NSAttributedString.Key.font: UIFont.avenirThin(WithSize: 15), NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.5411764706, green: 0.5411764706, blue: 0.5607843137, alpha: 1)])
    }
    func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
        if isFromCurrentSender(message: message) {
            let objMessageData = messageList[indexPath.section].arrChat[indexPath.row]
            if objMessageData.msgStatus == TagMsgStatus.read
            {
                let strStatus = objMessageData.msgStatus.rawValue + " " + (objMessageData.readTime ?? "")
                return NSAttributedString(string: strStatus, attributes: [NSAttributedString.Key.font: UIFont.avenirMedium(WithSize: 11.0,shouldResize: true),NSAttributedString.Key.foregroundColor : #colorLiteral(red: 0.5215686275, green: 0.5568627451, blue: 0.6, alpha: 1)])
                
            }
            return NSAttributedString(string: objMessageData.msgStatus.rawValue, attributes: [NSAttributedString.Key.font: UIFont.avenirMedium(WithSize: 11.0,shouldResize: true),NSAttributedString.Key.foregroundColor : #colorLiteral(red: 0.5215686275, green: 0.5568627451, blue: 0.6, alpha: 1)])
        }
        return nil
    }

Expected behavior
Application will not crash and it will show the typing indicator

Screenshots
Video
https://drive.google.com/file/d/18HkVKD6P3VSSi8uwlySGokGRMMfHWler/view?usp=sharing

Environment

  • Version of MessageKit : 3.1.0
  • Version of iOS : iOS 12
  • Version of Swift : swift 5
  • What device(s) are you testing on? : simulators , iPhone 5se
@kinoroy
Copy link
Member

kinoroy commented Jun 25, 2020

Hi @shahchirag2110

You point to the crash at a point where there could be an out of bounds array access. Could you share a stack trace of the crash or the error message you receive? The example app in the AdvancedExampleViewController uses a custom cell and the typing indicator together without issue, so it shouldn't be that alone.

@shahchirag2110
Copy link
Author

@kinoroy

Yes i totally agree with you that in AdvancedExampleViewController it totally working fine. and i take the code from their also but my issue is that i have section header so i return number of row in section where in demo you create section for particular section so my question is does this create any issue?

@shahchirag2110
Copy link
Author

Finally i solved the crash it is happening because one section and row was reserved for typing indicator which is make the index out of bound so I add one more dummy model in messageForItem(at indexPath , in messagesCollectionView) method when typing indicator was shown.

func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        if indexPath.section < messageList.count && indexPath.row < messageList[indexPath.section].arrChat.count {
            return messageList[indexPath.section].arrChat[indexPath.row]
        } else {
            return ChatModel.init() // dummy model inserted here
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants