Skip to content

Commit 854128b

Browse files
Updated caching utils
1 parent cc88f30 commit 854128b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
124124
@objc
125125
private func didReceiveMemoryWarning() {
126126
Nuke.ImageCache.shared.removeAll()
127+
messageCachingUtils.clearCache()
127128
}
128129

129130
public func scrollToLastMessage() {
@@ -322,6 +323,10 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
322323
}
323324
}
324325
}
326+
327+
deinit {
328+
messageCachingUtils.clearCache()
329+
}
325330
}
326331

327332
extension ChatMessage: Identifiable {

Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import Foundation
66
import StreamChat
77

8+
/// Caches messages related data to avoid accessing the database.
9+
/// Cleared on chat channel view dismiss or memory warning.
810
class MessageCachingUtils {
911

10-
private var messageAuthorMapping = [String: UserDisplayInfo]()
12+
private var messageAuthorMapping = [String: String]()
13+
private var messageAuthors = [String: UserDisplayInfo]()
1114
private var messageAttachments = [String: Bool]()
1215
private var checkedMessageIds = Set<String>()
1316
private var quotedMessageMapping = [String: ChatMessage]()
1417

1518
func authorId(for message: ChatMessage) -> String {
16-
if let userDisplayInfo = messageAuthorMapping[message.id] {
19+
if let userDisplayInfo = userDisplayInfo(for: message) {
1720
return userDisplayInfo.id
1821
}
1922

@@ -22,7 +25,7 @@ class MessageCachingUtils {
2225
}
2326

2427
func authorName(for message: ChatMessage) -> String {
25-
if let userDisplayInfo = messageAuthorMapping[message.id] {
28+
if let userDisplayInfo = userDisplayInfo(for: message) {
2629
return userDisplayInfo.name
2730
}
2831

@@ -31,7 +34,7 @@ class MessageCachingUtils {
3134
}
3235

3336
func authorImageURL(for message: ChatMessage) -> URL? {
34-
if let userDisplayInfo = messageAuthorMapping[message.id] {
37+
if let userDisplayInfo = userDisplayInfo(for: message) {
3538
return userDisplayInfo.imageURL
3639
}
3740

@@ -58,14 +61,35 @@ class MessageCachingUtils {
5861
return quoted
5962
}
6063

64+
func clearCache() {
65+
log.debug("Clearing cached message data")
66+
messageAuthorMapping = [String: String]()
67+
messageAuthors = [String: UserDisplayInfo]()
68+
messageAttachments = [String: Bool]()
69+
checkedMessageIds = Set<String>()
70+
quotedMessageMapping = [String: ChatMessage]()
71+
}
72+
73+
// MARK: - private
74+
75+
private func userDisplayInfo(for message: ChatMessage) -> UserDisplayInfo? {
76+
if let userId = messageAuthorMapping[message.id],
77+
let userDisplayInfo = messageAuthors[userId] {
78+
return userDisplayInfo
79+
} else {
80+
return nil
81+
}
82+
}
83+
6184
private func saveUserDisplayInfo(for message: ChatMessage) -> UserDisplayInfo {
6285
let user = message.author
6386
let userDisplayInfo = UserDisplayInfo(
6487
id: user.id,
6588
name: user.name ?? user.id,
6689
imageURL: user.imageURL
6790
)
68-
messageAuthorMapping[message.id] = userDisplayInfo
91+
messageAuthorMapping[message.id] = user.id
92+
messageAuthors[user.id] = userDisplayInfo
6993

7094
return userDisplayInfo
7195
}

0 commit comments

Comments
 (0)