From 1f63ce02a0ed0aa8124b1151110d8f7dcf4e9a3f Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sat, 21 Apr 2018 19:17:56 -0400 Subject: [PATCH] Increase cache, remove dead code, add debug logging // FREEBIE --- .../HomeView/HomeViewController.m | 2 ++ SignalServiceKit/src/Contacts/TSThread.h | 7 ----- SignalServiceKit/src/Contacts/TSThread.m | 29 +++++++------------ 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index 519abfa88..bd7012831 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -947,6 +947,8 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; if (!_uiDatabaseConnection) { _uiDatabaseConnection = [OWSPrimaryStorage.sharedManager newDatabaseConnection]; + // default is 250 + _uiDatabaseConnection.objectCacheLimit = 500; [_uiDatabaseConnection beginLongLivedReadTransaction]; } return _uiDatabaseConnection; diff --git a/SignalServiceKit/src/Contacts/TSThread.h b/SignalServiceKit/src/Contacts/TSThread.h index 1b32f81a1..07b795bd9 100644 --- a/SignalServiceKit/src/Contacts/TSThread.h +++ b/SignalServiceKit/src/Contacts/TSThread.h @@ -65,13 +65,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (NSArray *)receivedMessagesForInvalidKey:(NSData *)key; -/** - * Returns whether or not the thread has unread messages. - * - * @return YES if it has unread TSIncomingMessages, NO otherwise. - */ -- (BOOL)hasUnreadMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction - NS_SWIFT_NAME(hasUnreadMessages(transaction:)); - (NSUInteger)unreadMessageCountWithTransaction:(YapDatabaseReadTransaction *)transaction NS_SWIFT_NAME(unreadMessageCount(transaction:)); diff --git a/SignalServiceKit/src/Contacts/TSThread.m b/SignalServiceKit/src/Contacts/TSThread.m index c9d87fceb..074dc7c0d 100644 --- a/SignalServiceKit/src/Contacts/TSThread.m +++ b/SignalServiceKit/src/Contacts/TSThread.m @@ -200,18 +200,6 @@ NS_ASSUME_NONNULL_BEGIN return count; } -- (BOOL)hasUnreadMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction -{ - TSInteraction *interaction = [self lastInteractionWithTransaction:transaction]; - BOOL hasUnread = NO; - - if ([interaction isKindOfClass:[TSIncomingMessage class]]) { - hasUnread = ![(TSIncomingMessage *)interaction wasRead]; - } - - return hasUnread; -} - - (NSArray> *)unseenMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction { NSMutableArray> *messages = [NSMutableArray new]; @@ -245,13 +233,9 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert([self unseenMessagesWithTransaction:transaction].count < 1); } -- (TSInteraction *)lastInteractionWithTransaction:(YapDatabaseReadTransaction *)transaction -{ - return [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId]; -} - - (TSInteraction *)lastInteractionForInboxWithTransaction:(YapDatabaseReadTransaction *)transaction { + __block NSUInteger missedCount = 0; __block TSInteraction *last = nil; [[transaction ext:TSMessageDatabaseViewExtensionName] enumerateRowsInGroup:self.uniqueId @@ -260,11 +244,20 @@ NS_ASSUME_NONNULL_BEGIN NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) { OWSAssert([object isKindOfClass:[TSInteraction class]]); - + + missedCount++; TSInteraction *interaction = (TSInteraction *)object; if ([TSThread shouldInteractionAppearInInbox:interaction]) { last = interaction; + + // For long ignored threads, with lots of SN changes this can get really slow. + // I see this in development because I have a lot of long forgotten threads with members + // who's test devices are constantly reinstalled. We could add a purpose-built DB view, + // but I think in the real world this is rare to be a hotspot. + if (missedCount > 50) { + DDLogWarn(@"%@ found last interaction for inbox after skipping %tu items", self.logTag, missedCount); + } *stop = YES; } }];