diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index f0fa0524f..c9e2bf489 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -509,6 +509,7 @@ C3548F0824456AB6009433A8 /* UIView+Wrapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3548F0724456AB6009433A8 /* UIView+Wrapping.swift */; }; C354E75A23FE2A7600CE22E3 /* BaseVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C354E75923FE2A7600CE22E3 /* BaseVC.swift */; }; C35D0DB525AE5F1200B6BF49 /* UIEdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35D0DB425AE5F1200B6BF49 /* UIEdgeInsets.swift */; }; + C35D76DB26606304009AA5FB /* ThreadUpdateBatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35D76DA26606303009AA5FB /* ThreadUpdateBatcher.swift */; }; C35E8AAE2485E51D00ACB629 /* IP2Country.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35E8AAD2485E51D00ACB629 /* IP2Country.swift */; }; C374EEE225DA26740073A857 /* LinkPreviewModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = C374EEE125DA26740073A857 /* LinkPreviewModal.swift */; }; C374EEEB25DA3CA70073A857 /* ConversationTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C374EEEA25DA3CA70073A857 /* ConversationTitleView.swift */; }; @@ -1487,6 +1488,7 @@ C3548F0724456AB6009433A8 /* UIView+Wrapping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Wrapping.swift"; sourceTree = ""; }; C354E75923FE2A7600CE22E3 /* BaseVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseVC.swift; sourceTree = ""; }; C35D0DB425AE5F1200B6BF49 /* UIEdgeInsets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIEdgeInsets.swift; sourceTree = ""; }; + C35D76DA26606303009AA5FB /* ThreadUpdateBatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadUpdateBatcher.swift; sourceTree = ""; }; C35E8AA22485C72300ACB629 /* SwiftCSV.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftCSV.framework; path = ThirdParty/Carthage/Build/iOS/SwiftCSV.framework; sourceTree = ""; }; C35E8AAD2485E51D00ACB629 /* IP2Country.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IP2Country.swift; sourceTree = ""; }; C374EEE125DA26740073A857 /* LinkPreviewModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkPreviewModal.swift; sourceTree = ""; }; @@ -3145,6 +3147,7 @@ C33FDB32255A580A00E217F9 /* SSKIncrementingIdFinder.swift */, C3A3A170256E1D25004D228D /* SSKReachabilityManager.swift */, C3ECBF7A257056B700EA7FCE /* Threading.swift */, + C35D76DA26606303009AA5FB /* ThreadUpdateBatcher.swift */, C33FDB5F255A580E00E217F9 /* YapDatabaseConnection+OWS.h */, C33FDB43255A580C00E217F9 /* YapDatabaseConnection+OWS.m */, C33FDA88255A57FD00E217F9 /* YapDatabaseTransaction+OWS.h */, @@ -4680,6 +4683,7 @@ B8856CEE256F1054001CE70E /* OWSAudioPlayer.m in Sources */, C32C5EDC256DF501003C73A2 /* YapDatabaseConnection+OWS.m in Sources */, C3BBE0762554CDA60050F1E3 /* Configuration.swift in Sources */, + C35D76DB26606304009AA5FB /* ThreadUpdateBatcher.swift in Sources */, B8B32033258B235D0020074B /* Storage+Contacts.swift in Sources */, B8856D69256F141F001CE70E /* OWSWindowManager.m in Sources */, C3D9E3BE25676AD70040E4F3 /* TSAttachmentPointer.m in Sources */, diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 177b60ba9..0d0eb6a23 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -25,9 +25,7 @@ extension MessageReceiver { // Touch the thread to update the home screen preview let storage = SNMessagingKitConfiguration.shared.storage guard let threadID = storage.getOrCreateThread(for: message.sender!, groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { return } - let transaction = transaction as! YapDatabaseReadWriteTransaction - guard let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return } - thread.touch(with: transaction) + ThreadUpdateBatcher.shared.touch(threadID) } diff --git a/SessionMessagingKit/Utilities/ThreadUpdateBatcher.swift b/SessionMessagingKit/Utilities/ThreadUpdateBatcher.swift new file mode 100644 index 000000000..3eebcbda9 --- /dev/null +++ b/SessionMessagingKit/Utilities/ThreadUpdateBatcher.swift @@ -0,0 +1,31 @@ + +final class ThreadUpdateBatcher { + private var threadIDs: Set = [] + + private lazy var timer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in self?.touch() } + + static let shared = ThreadUpdateBatcher() + + private init() { + DispatchQueue.main.async { + SessionUtilitiesKit.touch(self.timer) + } + } + + deinit { timer.invalidate() } + + func touch(_ threadID: String) { + threadIDs.insert(threadID) + } + + @objc private func touch() { + let threadIDs = self.threadIDs + self.threadIDs.removeAll() + Storage.write { transaction in + for threadID in threadIDs { + guard let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return } + thread.touch(with: transaction) + } + } + } +} diff --git a/SessionUtilitiesKit/General/General.swift b/SessionUtilitiesKit/General/General.swift index 08c97811c..804149346 100644 --- a/SessionUtilitiesKit/General/General.swift +++ b/SessionUtilitiesKit/General/General.swift @@ -1,4 +1,11 @@ +/// Does nothing, but is never inlined and thus evaluating its argument will never be optimized away. +/// +/// Useful for forcing the instantiation of lazy properties like globals. +@inline(never) +public func touch(_ value: Value) { /* Do nothing */ } + + /// Returns `f(x!)` if `x != nil`, or `nil` otherwise. public func given(_ x: T?, _ f: (T) throws -> U) rethrows -> U? { return try x.map(f) }