简体   繁体   中英

How to group chat message by date in swift using xmpp?

I'm trying to implement a chat application using XMPP. I was trying to group messages only by date, but timestamp contains "date with time".

Could someone please help me fix this issue.

lazy var fetchController: NSFetchedResultsController<NSFetchRequestResult>? = {
    if let fetchRequest = self.xmppManager?.getFetchRequestForArchievedMessages(nil), let moc = self.xmppManager?.getXMPPMocForMessages() {
        let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "timestamp", cacheName: nil)
        fetchedResultsController.delegate = self
        return fetchedResultsController
    }
    return nil
}()

Thanks in advance.

As suggested, I wrote computed property

extension XMPPMessageArchiving_Message_CoreDataObject {
var dateAsSection: String {
    get {
        return self.timestamp.toString(withFormat: "dd/mm/yyyy")
    }
} 

But terminates with

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity XMPPMessageArchiving_Message_CoreDataObject is not key value coding-compliant for the key "dateAsSection".'

Finally got the solution. Thanks to @Joakim Danielson for helping me out.

 lazy var fetchController: NSFetchedResultsController<NSFetchRequestResult>? = {
    if let fetchRequest = self.xmppManager?.getFetchRequestForArchievedMessages(nil), let moc = self.xmppManager?.getXMPPMocForMessages() {
        let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: **#keyPath(XMPPMessageArchiving_Message_CoreDataObject.dateAsSection)**, cacheName: nil)
        fetchedResultsController.delegate = self
        return fetchedResultsController
    }
    return nil
}()

computed property has to be added to the extension

extension XMPPMessageArchiving_Message_CoreDataObject {
@objc var dateAsSection : Date {
    get {
        return self.timestamp.toString(withFormat: "dd/MM/yyyy").toDate(withFormat: "dd/MM/yyyy")!
    }
}
}

Worked for me...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM