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
NSFetchedResultsController and sections #11
Comments
Hey @machukas, when you initially fetch your messages (I assume you fetch what's stored locally and then you handle anything incoming later), you could process them with a function like this: func fetchComments() {
do {
try frc.performFetch()
if let messages = frc.fetchedObjects {
self.addMessages(messages)
}
} catch {
print(error.localizedDescription)
}
}
public func addMessages(_ messages: [MSGMessage]) {
collectionView.performBatchUpdates({
comments.forEach { comment in
id += 1
if let lastSection = self.messages.last, let lastMessage = lastSection.last, lastMessage.user.displayName == message.user.displayName {
self.messages[self.messages.count - 1].append(message)
let sectionIndex = self.messages.count - 1
let itemIndex = self.messages[sectionIndex].count - 1
self.collectionView.insertItems(at: [IndexPath(item: itemIndex, section: sectionIndex)])
} else {
self.messages.append([message])
let sectionIndex = self.messages.count - 1
self.collectionView.insertSections([sectionIndex])
}
}
}, completion: { (_) in
self.collectionView.scrollToBottom(animated: true)
self.collectionView.layoutTypingLabelIfNeeded()
})
} You could then do the same thing with a similar method that takes a single comment arguments, when you send or receive a message: override func inputViewPrimaryActionTriggered(inputView: MSGInputView) {
addMessage(inputView.message)
} |
Hey @sjoness, thanks for replying. What's self.messages? Are you not using the frc.object(at:) as dataSource? |
@AraTechES a suggestion would be to fetch the messages once and then store the results in a separate property so that you can manipulate the order of the messages. In this example you would use the |
Understood, thanks man. |
Hey @machukas, I have been trying to figure this thing out but I'm completely stuck. How did you implement this? |
To be honest, I didn't. I had to use FRC as a requisite, so I couldn't go with sjoness suggestion. |
Hi @steve228uk,
First of all thanks for this super easy to use library!
I am using it with a NSFetchedResultsController, and I am struggling myself trying to get by free the messages grouped by username after ordered by timestamp, but I am not succeeding.
If I provide the date as sort descriptor, and the user as sectionNameKeyPath, it groups the messages by user, so I get numberOfUsers sections, and ordered by timeStamp inside, but that's not what I want; I want by date, but grouped by user just if the messages are in a sequence, like your example.
I guess you (or someone) has faced this problem before, I hope haha.
Thanks in advance
The text was updated successfully, but these errors were encountered: