Load attachments for conversation view items using long-lived db connection.

This commit is contained in:
Matthew Chen 2017-11-16 11:22:16 -05:00
parent 5d5f7f0157
commit ddf4bf28cc
4 changed files with 36 additions and 26 deletions

View File

@ -4007,7 +4007,9 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
if (viewItem) {
viewItem.previousRow = viewItem.row;
} else {
viewItem = [[ConversationViewItem alloc] initWithTSInteraction:interaction isGroupThread:isGroupThread];
viewItem = [[ConversationViewItem alloc] initWithInteraction:interaction
isGroupThread:isGroupThread
transaction:transaction];
}
viewItem.row = (NSInteger)row;
[viewItems addObject:viewItem];
@ -4125,7 +4127,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
if (!interaction) {
OWSFail(@"%@ could not reload interaction", self.logTag);
} else {
[viewItem replaceInteraction:interaction];
[viewItem replaceInteraction:interaction transaction:transaction];
}
}];
}

View File

@ -30,6 +30,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType);
@class TSAttachmentPointer;
@class TSAttachmentStream;
@class TSInteraction;
@class YapDatabaseReadTransaction;
// This is a ViewModel for cells in the conversation view.
//
@ -55,15 +56,15 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType);
// previous update.
@property (nonatomic) NSInteger previousRow;
//@property (nonatomic, weak) ConversationViewCell *lastCell;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithTSInteraction:(TSInteraction *)interaction isGroupThread:(BOOL)isGroupThread;
- (instancetype)initWithInteraction:(TSInteraction *)interaction
isGroupThread:(BOOL)isGroupThread
transaction:(YapDatabaseReadTransaction *)transaction;
- (ConversationViewCell *)dequeueCellForCollectionView:(UICollectionView *)collectionView
indexPath:(NSIndexPath *)indexPath;
- (void)replaceInteraction:(TSInteraction *)interaction;
- (void)replaceInteraction:(TSInteraction *)interaction transaction:(YapDatabaseReadTransaction *)transaction;
- (void)clearCachedLayoutState;

View File

@ -63,7 +63,9 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
@implementation ConversationViewItem
- (instancetype)initWithTSInteraction:(TSInteraction *)interaction isGroupThread:(BOOL)isGroupThread
- (instancetype)initWithInteraction:(TSInteraction *)interaction
isGroupThread:(BOOL)isGroupThread
transaction:(YapDatabaseReadTransaction *)transaction
{
self = [super init];
@ -76,10 +78,12 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
self.row = NSNotFound;
self.previousRow = NSNotFound;
[self ensureViewState:transaction];
return self;
}
- (void)replaceInteraction:(TSInteraction *)interaction
- (void)replaceInteraction:(TSInteraction *)interaction transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssert(interaction);
@ -93,6 +97,8 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
self.contentSize = CGSizeZero;
[self clearCachedLayoutState];
[self ensureViewState:transaction];
}
- (void)setShouldShowDate:(BOOL)shouldShowDate
@ -332,7 +338,10 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
}
- (nullable TSAttachment *)firstAttachmentIfAnyOfMessage:(TSMessage *)message
transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssert(transaction);
if (message.attachmentIds.count == 0) {
return nil;
}
@ -340,21 +349,22 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
if (attachmentId.length == 0) {
return nil;
}
return [TSAttachment fetchObjectWithUniqueID:attachmentId];
return [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
}
- (void)ensureViewState
- (void)ensureViewState:(YapDatabaseReadTransaction *)transaction
{
OWSAssert([NSThread isMainThread]);
OWSAssert(transaction);
OWSAssert(!self.hasViewState);
OWSAssert([self.interaction isKindOfClass:[TSOutgoingMessage class]] ||
[self.interaction isKindOfClass:[TSIncomingMessage class]]);
if (self.hasViewState) {
return;
}
self.hasViewState = YES;
TSMessage *message = (TSMessage *)self.interaction;
TSAttachment *_Nullable attachment = [self firstAttachmentIfAnyOfMessage:message];
TSAttachment *_Nullable attachment = [self firstAttachmentIfAnyOfMessage:message transaction:transaction];
if (attachment) {
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
self.attachmentStream = (TSAttachmentStream *)attachment;
@ -418,16 +428,13 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
{
OWSAssert([NSThread isMainThread]);
[self ensureViewState];
return _messageCellType;
}
- (nullable DisplayableText *)displayableText
{
OWSAssert([NSThread isMainThread]);
[self ensureViewState];
OWSAssert(self.hasViewState);
OWSAssert(_displayableText);
OWSAssert(_displayableText.displayText);
@ -439,8 +446,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (nullable TSAttachmentStream *)attachmentStream
{
OWSAssert([NSThread isMainThread]);
[self ensureViewState];
OWSAssert(self.hasViewState);
return _attachmentStream;
}
@ -448,8 +454,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (nullable TSAttachmentPointer *)attachmentPointer
{
OWSAssert([NSThread isMainThread]);
[self ensureViewState];
OWSAssert(self.hasViewState);
return _attachmentPointer;
}
@ -457,8 +462,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (CGSize)contentSize
{
OWSAssert([NSThread isMainThread]);
[self ensureViewState];
OWSAssert(self.hasViewState);
return _contentSize;
}

View File

@ -48,7 +48,10 @@
TSOutgoingMessage *message =
[[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:self.fakeTextMessageText];
[message save];
ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithTSInteraction:message isGroupThread:NO];
__block ConversationViewItem *viewItem = nil;
[TSYapDatabaseObject.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
viewItem = [[ConversationViewItem alloc] initWithInteraction:message isGroupThread:NO transaction:transaction];
}];
return viewItem;
}
@ -72,7 +75,7 @@
TSOutgoingMessage *message =
[[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:nil attachmentIds:attachmentIds];
[message save];
ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithTSInteraction:message isGroupThread:NO];
ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithInteraction:message isGroupThread:NO];
return viewItem;
}