Twilio : Sort Channel based on last message time

喜你入骨 提交于 2020-07-07 10:59:09

问题


I am using Twilio chat SDK in my iPhone application. I am trying to sort all channels list based on last message timestamp, is there any way to achieve this? Please suggest.


回答1:


Twilio developer evangelist here.

The SDK doesn't currently have a way to sort channels. Your best bet is to load all the channels into an array and sort them yourself.




回答2:


As @philnash said, currently the SDK doesn't have a way to sort channels. This is how I have sort on my own in javascript. In javascript SDK, the channel has the last message's timestamp which I used for sorting. No need to fetch the last message and then look for it's timestamp.

const sortedChannels = channels.sort(function (a, b) {

 /** Sort based on the last message if not, consider the last update of the channel */
 return new Date(b.lastMessage ? b.lastMessage.timestamp : b.dateUpdated) - new 
 Date(a.lastMessage ?
    a.lastMessage.timestamp :
    a.dateCreated);
 });



回答3:


I solved the issue like this:

    func sortChannels() {
        let sortSelector = #selector(NSDate.compare(_:))
        let descriptor = NSSortDescriptor(key: "dateUpdatedAsDate", ascending: false, selector: sortSelector)
        channels!.sort(using: [descriptor])
    }

Edit:
Twilio documentation is kind of confusing. Maybe a better solution is using lastMessageDate, as dateUpdatedAsDate seems to be for something else.

NSSortDescriptor(key: "lastMessageDate", ascending: false, selector: sortSelector)


来源:https://stackoverflow.com/questions/48162278/twilio-sort-channel-based-on-last-message-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!