Skip redundant sync messages.

This commit is contained in:
Matthew Chen 2018-01-10 12:59:02 -05:00
parent 41e6eaeafc
commit a2b67a17fd
3 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "TSYapDatabaseObject.h"
@ -17,6 +17,7 @@ extern uint32_t const OWSDevicePrimaryDeviceId;
- (BOOL)mayHaveLinkedDevices:(YapDatabaseConnection *)dbConnection;
- (void)setMayHaveLinkedDevices:(BOOL)value dbConnection:(YapDatabaseConnection *)dbConnection;
- (void)setMayHaveLinkedDevices:(BOOL)value transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds;
- (void)setHasReceivedSyncMessage;

View File

@ -19,7 +19,6 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
@interface OWSDeviceManager ()
@property (atomic, nullable) NSNumber *mayHaveLinkedDevicesCached;
@property (atomic) NSDate *lastReceivedSyncMessage;
@end
@ -49,13 +48,9 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
@synchronized(self)
{
if (!self.mayHaveLinkedDevicesCached) {
self.mayHaveLinkedDevicesCached = @([dbConnection boolForKey:kTSStorageManager_MayHaveLinkedDevices
inCollection:kTSStorageManager_OWSDeviceCollection
defaultValue:NO]);
}
return [self.mayHaveLinkedDevicesCached boolValue];
return [dbConnection boolForKey:kTSStorageManager_MayHaveLinkedDevices
inCollection:kTSStorageManager_OWSDeviceCollection
defaultValue:NO];
}
}
@ -63,15 +58,20 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
{
OWSAssert(dbConnection);
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[self setMayHaveLinkedDevices:value transaction:transaction];
}];
}
- (void)setMayHaveLinkedDevices:(BOOL)value transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(transaction);
@synchronized(self)
{
self.mayHaveLinkedDevicesCached = @(value);
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[transaction setObject:@(value)
forKey:kTSStorageManager_MayHaveLinkedDevices
inCollection:kTSStorageManager_OWSDeviceCollection];
}];
[transaction setObject:@(value)
forKey:kTSStorageManager_MayHaveLinkedDevices
inCollection:kTSStorageManager_OWSDeviceCollection];
}
}

View File

@ -278,6 +278,7 @@ NS_ASSUME_NONNULL_BEGIN
[self handleIncomingEnvelope:envelope withSyncMessage:content.syncMessage transaction:transaction];
[[OWSDeviceManager sharedManager] setHasReceivedSyncMessage];
[OWSDeviceManager.sharedManager setMayHaveLinkedDevices:YES transaction:transaction];
} else if (content.hasDataMessage) {
[self handleIncomingEnvelope:envelope withDataMessage:content.dataMessage transaction:transaction];
} else if (content.hasCallMessage) {