diff --git a/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m b/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m index 4fbb680fa..53afc1892 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m +++ b/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m @@ -6,12 +6,21 @@ #define TSStorageManagerSessionStoreCollection @"TSStorageManagerSessionStoreCollection" -@implementation TSStorageManager (SessionStore) +void AssertIsOnSessionStoreQueue() +{ + if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) { + dispatch_assert_queue([OWSDispatch sessionStoreQueue]); + } // else, skip assert as it's a development convenience. +} +@implementation TSStorageManager (SessionStore) #pragma mark - SessionStore -- (SessionRecord *)loadSession:(NSString *)contactIdentifier deviceId:(int)deviceId { +- (SessionRecord *)loadSession:(NSString *)contactIdentifier deviceId:(int)deviceId +{ + AssertIsOnSessionStoreQueue(); + NSDictionary *dictionary = [self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; @@ -28,7 +37,10 @@ return record; } -- (NSArray *)subDevicesSessions:(NSString *)contactIdentifier { +- (NSArray *)subDevicesSessions:(NSString *)contactIdentifier +{ + AssertIsOnSessionStoreQueue(); + NSDictionary *dictionary = [self objectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; @@ -45,7 +57,10 @@ return subDevicesSessions; } -- (void)storeSession:(NSString *)contactIdentifier deviceId:(int)deviceId session:(SessionRecord *)session { +- (void)storeSession:(NSString *)contactIdentifier deviceId:(int)deviceId session:(SessionRecord *)session +{ + AssertIsOnSessionStoreQueue(); + // We need to ensure subsequent usage of this SessionRecord does not consider this session as "fresh". Normally this // is achieved by marking things as "not fresh" at the point of deserialization - when we fetch a SessionRecord from // YapDB (initWithCoder:). However, because YapDB has an object cache, rather than fetching/deserializing, it's @@ -65,11 +80,17 @@ [self setObject:dictionary forKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; } -- (BOOL)containsSession:(NSString *)contactIdentifier deviceId:(int)deviceId { +- (BOOL)containsSession:(NSString *)contactIdentifier deviceId:(int)deviceId +{ + AssertIsOnSessionStoreQueue(); + return [self loadSession:contactIdentifier deviceId:deviceId].sessionState.hasSenderChain; } -- (void)deleteSessionForContact:(NSString *)contactIdentifier deviceId:(int)deviceId { +- (void)deleteSessionForContact:(NSString *)contactIdentifier deviceId:(int)deviceId +{ + AssertIsOnSessionStoreQueue(); + NSMutableDictionary *dictionary = [[self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection] mutableCopy]; @@ -82,7 +103,10 @@ [self setObject:dictionary forKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; } -- (void)deleteAllSessionsForContact:(NSString *)contactIdentifier { +- (void)deleteAllSessionsForContact:(NSString *)contactIdentifier +{ + AssertIsOnSessionStoreQueue(); + [self removeObjectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; } @@ -96,6 +120,8 @@ - (void)printAllSessions { + AssertIsOnSessionStoreQueue(); + NSString *tag = @"[TSStorageManager (SessionStore)]"; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { DDLogDebug(@"%@ All Sessions:", tag);