Hello,
My method to add a message looks like the following:
func addMessage(withText text: String, forSender sender: Conversations.MessageSender) {
messageList.append(ConversationMessage(text: text, sender: sender, messageId: "oMessage", date: Date()))
messagesCollectionView.reloadData()
messagesCollectionView.scrollToBottom(animated: true)
}
How do I add a delay to messages appearing?
I have multiple messages incoming and I am iterating over them like this:
for message in conversation.messages {
self.messageKitConversationViewController?.addMessage(withText: message.body!, forSender: message.sender!)
}
No matter how I am adding the delay (with sleep or DispatchQueue.main.asyncAfter, it waits and then appends all the messages at the same time. If I perform the delay with sync, I get an error from reloadData()
Thanks!
Don't use reloadData, use insertSection as we do in the Example project
Sent with GitHawk
@nathantannar4 Ok - I switched it out. I'm still unable to cause a delay for each message though :( same problems as above.
Are you saying you want to add a delay for adding each message ? Such that when they render out on the collection view they all fade in? You will need to be more specific as to why you want this use case
Sent with GitHawk
@nathantannar4 Yes, as in if there's 3 messages incoming, I want to add message 1, wait 1 second, add message 2, wait 1 second, then add message 3. However I've tried it, the for loop waits 3 seconds then puts out all 3 messages into the collection view at the same time, which is not what I want.
This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Any updates?
@emperorhaplo This is client specific logic, you need to use DispatchQueue. You cannot just loop though as they will all be inserted right away as there is no delay.
This is an EXAMPLE. You should lookup how to use DispatchQueue to get the results you want.
for i in 0..<10 {
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + Double(i)) {
DispatchQueue.main.async {
// Insert Message
}
}
}
@nathantannar4 this is not working as I mentioned in my initial issue - it is waiting the entire duration and then appending all messages at once.
e.g. if the wait time to the DispatchQueue is 1 second and there's 3 messages, it is waiting 3 seconds then appending ALL messages at the same time instead of doing it one at a time.
I am able to achieve the desired effect. You are doing something wrong client side. Sorry this isn't an issue specific to MessageKit
Sent with GitHawk
@nathantannar4 the code you pasted actually helped a lot - the problem was the deadline. I was adding a constant to the deadline instead of the index like you were :) Thank you!
Most helpful comment
@nathantannar4 the code you pasted actually helped a lot - the problem was the deadline. I was adding a constant to the deadline instead of the index like you were :) Thank you!