Assert that session mutation occurs on serial queue

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent 7578176e37
commit 9a444f4288

@ -6,12 +6,21 @@
#define TSStorageManagerSessionStoreCollection @"TSStorageManagerSessionStoreCollection" #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 #pragma mark - SessionStore
- (SessionRecord *)loadSession:(NSString *)contactIdentifier deviceId:(int)deviceId { - (SessionRecord *)loadSession:(NSString *)contactIdentifier deviceId:(int)deviceId
{
AssertIsOnSessionStoreQueue();
NSDictionary *dictionary = NSDictionary *dictionary =
[self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; [self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection];
@ -28,7 +37,10 @@
return record; return record;
} }
- (NSArray *)subDevicesSessions:(NSString *)contactIdentifier { - (NSArray *)subDevicesSessions:(NSString *)contactIdentifier
{
AssertIsOnSessionStoreQueue();
NSDictionary *dictionary = NSDictionary *dictionary =
[self objectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; [self objectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection];
@ -45,7 +57,10 @@
return subDevicesSessions; 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 // 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 // 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 // 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]; [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; 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 = NSMutableDictionary *dictionary =
[[self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection] mutableCopy]; [[self dictionaryForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection] mutableCopy];
@ -82,7 +103,10 @@
[self setObject:dictionary forKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; [self setObject:dictionary forKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection];
} }
- (void)deleteAllSessionsForContact:(NSString *)contactIdentifier { - (void)deleteAllSessionsForContact:(NSString *)contactIdentifier
{
AssertIsOnSessionStoreQueue();
[self removeObjectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection]; [self removeObjectForKey:contactIdentifier inCollection:TSStorageManagerSessionStoreCollection];
} }
@ -96,6 +120,8 @@
- (void)printAllSessions - (void)printAllSessions
{ {
AssertIsOnSessionStoreQueue();
NSString *tag = @"[TSStorageManager (SessionStore)]"; NSString *tag = @"[TSStorageManager (SessionStore)]";
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
DDLogDebug(@"%@ All Sessions:", tag); DDLogDebug(@"%@ All Sessions:", tag);

Loading…
Cancel
Save