From a8a96d64019405c6f4ef962b71ce4b1bf8804472 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 30 Jan 2018 23:54:28 -0600 Subject: [PATCH] Replace dictionary with NSCache #495 --- .../MessagesCollectionViewFlowLayout.swift | 21 +++++++++++-------- .../Cells/MessageCollectionViewCell.swift | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index db414e78..67656bfa 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -57,13 +57,17 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { /// Determines the maximum number of `MessageCollectionViewCell` attributes to cache. /// /// The default value of this property is 500. - open var attributesCacheMaxSize: Int = 500 + open var attributesCacheMaxSize: Int = 500 { + didSet { + layoutContextCache.countLimit = attributesCacheMaxSize + } + } - typealias MessageID = String + typealias MessageID = NSString /// The cache for `MessageCellLayoutContext`. /// The key is the `messageId` of the `MessageType`. - fileprivate var layoutContextCache: [MessageID: MessageCellLayoutContext] = [:] + fileprivate var layoutContextCache = NSCache() /// The `MessageCellLayoutContext` for the current cell. var currentLayoutContext: MessageCellLayoutContext! @@ -108,12 +112,12 @@ extension MessagesCollectionViewFlowLayout { /// - Parameters: /// - messageId: The `messageId` for the `MessageType` whose cached layout information is to be removed. public func removeCachedAttributes(for messageId: String) { - layoutContextCache.removeValue(forKey: messageId) + layoutContextCache.removeObject(forKey: messageId as NSString) } /// Removes the cached layout information for all `MessageType`s. public func removeAllCachedAttributes() { - layoutContextCache.removeAll() + layoutContextCache.removeAllObjects() } open override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { @@ -207,12 +211,11 @@ extension MessagesCollectionViewFlowLayout { extension MessagesCollectionViewFlowLayout { internal func cellLayoutContext(for message: MessageType, at indexPath: IndexPath) -> MessageCellLayoutContext { - guard let cachedContext = layoutContextCache[message.messageId] else { + guard let cachedContext = layoutContextCache.object(forKey: message.messageId as NSString) else { let newContext = newCellLayoutContext(for: message, at: indexPath) - let shouldCache = messagesLayoutDelegate.shouldCacheLayoutAttributes(for: message) - if shouldCache && layoutContextCache.count < attributesCacheMaxSize { - layoutContextCache[message.messageId] = newContext + if messagesLayoutDelegate.shouldCacheLayoutAttributes(for: message) { + layoutContextCache.setObject(newContext, forKey: message.messageId as NSString) } return newContext } diff --git a/Sources/Views/Cells/MessageCollectionViewCell.swift b/Sources/Views/Cells/MessageCollectionViewCell.swift index 79b60f8e..fed56077 100644 --- a/Sources/Views/Cells/MessageCollectionViewCell.swift +++ b/Sources/Views/Cells/MessageCollectionViewCell.swift @@ -149,7 +149,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab open func layoutAvatarView(with attributes: MessagesCollectionViewLayoutAttributes) { guard attributes.avatarSize != .zero else { return } - var origin = CGPoint.zero + var origin: CGPoint = .zero switch attributes.avatarPosition.horizontal { case .cellLeading: @@ -201,7 +201,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab let topLabelPadding = topLabelAlignment.insets let topLabelSize = attributes.topLabelSize - var origin = CGPoint.zero + var origin: CGPoint = .zero origin.y = topLabelPadding.top