gpt4 book ai didi

ios - 使用日期字符串Swift解析大型json数据时的CPU使用率高

转载 作者:行者123 更新时间:2023-12-01 19:31:53 25 4
gpt4 key购买 nike

我正在研究聊天应用程序。我得到包含与对话相关的数据的对话列表。 Json响应如下所示

[
{
"id": "1021",
"title": "Test Group",
"memberCount": 250,
"isGroup": true,
"ownerId": "18646",
"createdAt": "2017-08-24T12:05:06.000Z",
"type": "private",
"lastReadAt": {
"18646": "2020-01-29T06:48:53.700Z",
"18664": "2020-01-29T07:12:41.613Z",
"18714": "2020-01-29T07:12:41.613Z",
// 247 More Records
},
"unreadMessageCount": 0,
"mute": false,
"isActive": true,
"isAdmin": true,
"isDeleted": false,
"updatedAt": "2020-01-29T07:12:41.124Z",
"lastMessage": {
"id": "18a16164-657c-420a-887d-5408f1479619",
"conversationId": "1021",
"type": "normal",
"createdAt": "2020-01-29T07:12:41.124Z",
"updatedAt": "2020-01-29T07:12:41.124Z",
"body": "hi",
"ownerId": "18646",
"attachments": [],
"owner": {
"id": "18646",
"displayName": "sydney millers",
"profileImageUrl": "https://sample-file-url/media/fbef1bb47eb85fb2979b135ed04e0eb3.png"
},
"mentionedUsers": []
}
},
{
"id": "1021",
"title": "Test Group 2",
"memberCount": 250,
"isGroup": true,
"ownerId": "18646",
"createdAt": "2017-08-24T12:05:06.000Z",
"type": "private",
"lastReadAt": {
"18646": "2020-01-29T06:48:53.700Z",
"18664": "2020-01-29T07:12:41.613Z",
"18714": "2020-01-29T07:12:41.613Z",
// 247 More Records
},
"unreadMessageCount": 0,
"mute": false,
"isActive": true,
"isAdmin": true,
"isDeleted": false,
"updatedAt": "2020-01-29T07:12:41.124Z",
"lastMessage": {
"id": "18a16164-657c-420a-887d-5408f1479619",
"conversationId": "1021",
"type": "normal",
"createdAt": "2020-01-29T07:12:41.124Z",
"updatedAt": "2020-01-29T07:12:41.124Z",
"body": "hi",
"ownerId": "18646",
"attachments": [],
"owner": {
"id": "18646",
"displayName": "sydney millers",
"profileImageUrl": "https://sample-file-url/media/fbef1bb47eb85fb2979b135ed04e0eb3.png"
},
"mentionedUsers": []
}
}
]

当组成员最后一次阅读对话时, lastReadAt基本包含信息。现在要获取最后一条消息状态(无论是否已全部读取)并将其放在表单元格中,我必须找到最早的日期并将其与消息日期进行比较。但是要比较日期,我必须将它们从String转换为Date。现在假设有10个对话,每个对话包含250个成员。因此,有很多任务要做,所以我的CPU使用率达到了100%。任何更好的解决方案。为了快速我正在使用以下功能
func getMessageStatus(itemMessageDate: Date) -> BaseMessageStatus {
let dateTransformer = ISODateTransform() // Custom Date Transformer Same as of ios default but with a fixed date format.
if let lastReadInfoDic = self.conversation?.lastReadDictionary {
var lastReadData = [String:Date]()
lastReadInfoDic.forEach({(id,date) in
if let memberReadDate = dateTransformer.transformFromJSON(date) {
lastReadData.updateValue(memberReadDate, forKey: id)
}
})
let sortedData = lastReadData.sorted(by: {$0.value < $1.value})
if let oldestReader = sortedData.first {
let oldestReadDate = oldestReader.value
if itemMessageDate <= oldestReadDate {
return .seen
} else {
return .sent
}
}
}
return .sent
}

最佳答案

如果我正确阅读了此书,则需要self.conversation?.lastReadDictionary中的最短日期。看起来lastReadDictionary[String: String]

如果是这样,请使用compactMapValues(_:)[String: String]转换为[String: Date]。接下来,使用min(_:)查找最早的日期。

func getMessageStatus(itemMessageDate: Date) -> BaseMessageStatus {
let dateTransformer = ISODateTransform()

let oldestReader = conversation?.lastReadDictionary
.compactMapValues { dateTransformer.transformFromJSON($0) }
.min { $0.value < $1.value } // NOTE: Comparing Date objects.

guard let oldestDate = oldestReader?.value else {
return .sent
}

if itemMessageDate <= oldestDate {
return .seen
} else {
return .sent
}
}

所有时间字符串都使用 Z时区吗?如果是这样,则可以使用字符串比较来查找最早的ISO时间字符串。拥有最早的字符串后,然后将该一个ISO时间字符串转换为 Date

func getMessageStatus(itemMessageDate: Date) -> BaseMessageStatus {
let dateTransformer = ISODateTransform()

let oldestElement = conversation?.lastReadDictionary
.min { $0.value < $1.value } // NOTE: Comparing String objects.

guard let string = oldestElement?.value, let oldestDate = dateTransformer.transformFromJSON(string) else {
return .sent
}

if itemMessageDate <= oldestDate {
return .seen
} else {
return .sent
}
}

关于ios - 使用日期字符串Swift解析大型json数据时的CPU使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62360446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com