【发布时间】:2018-01-09 06:05:06
【问题描述】:
我在我的 iPhone 应用程序中使用 Twilio 聊天 SDK。 我正在尝试根据最后一条消息时间戳对所有频道列表进行排序,有没有办法实现这一点?请提出建议。
【问题讨论】:
标签: ios twilio twilio-api
我在我的 iPhone 应用程序中使用 Twilio 聊天 SDK。 我正在尝试根据最后一条消息时间戳对所有频道列表进行排序,有没有办法实现这一点?请提出建议。
【问题讨论】:
标签: ios twilio twilio-api
这里是 Twilio 开发者宣传员。
SDK 目前无法对频道进行排序。最好的办法是将所有通道加载到一个数组中并自行排序。
【讨论】:
setAttributes方法。
正如@philnash 所说,目前 SDK 没有办法对频道进行排序。这就是我在javascript 中自行排序的方式。在 javascript SDK 中,通道具有我用于排序的最后一条消息的时间戳。
无需获取最后一条消息然后查找它的时间戳。
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);
});
【讨论】:
我解决了这样的问题:
func sortChannels() {
let sortSelector = #selector(NSDate.compare(_:))
let descriptor = NSSortDescriptor(key: "dateUpdatedAsDate", ascending: false, selector: sortSelector)
channels!.sort(using: [descriptor])
}
编辑:
Twilio 文档有点令人困惑。也许更好的解决方案是使用lastMessageDate,因为dateUpdatedAsDate 似乎是为了别的东西。
NSSortDescriptor(key: "lastMessageDate", ascending: false, selector: sortSelector)
【讨论】: