Apply assert to ensure singletons are only created once.

// FREEBIE
pull/1/head
Matthew Chen 9 years ago
parent ec7a796b71
commit cd4134c9da

@ -17,11 +17,13 @@ typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) {
*/
@interface TSAttachmentPointer : TSAttachment
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithServerId:(UInt64)serverId
key:(NSData *)key
digest:(nullable NSData *)digest
contentType:(NSString *)contentType
relay:(NSString *)relay NS_DESIGNATED_INITIALIZER;
relay:(NSString *)relay;
@property (nonatomic, readonly) NSString *relay;
@property (atomic) TSAttachmentPointerState state;

@ -66,6 +66,8 @@ NSString *const kTSStorageManager_SyncedBlockedPhoneNumbersKey = @"kTSStorageMan
_storageManager = storageManager;
_messageSender = messageSender;
OWSSingletonAssert();
return self;
}

@ -106,6 +106,8 @@ NS_ASSUME_NONNULL_BEGIN
_incomingMessageFinder = [[OWSIncomingMessageFinder alloc] initWithDatabase:storageManager.database];
_blockingManager = [TSBlockingManager sharedManager];
OWSSingletonAssert();
return self;
}

@ -1,9 +1,5 @@
//
// TSSocketManager.h
// TextSecureiOS
//
// Created by Frederic Jacobs on 17/05/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import <Foundation/Foundation.h>
@ -23,6 +19,8 @@ extern NSString *const SocketConnectingNotification;
@interface TSSocketManager : NSObject <SRWebSocketDelegate>
- (instancetype)init NS_UNAVAILABLE;
+ (void)becomeActiveFromForeground;
+ (void)becomeActiveFromBackgroundExpectMessage:(BOOL)expected;

@ -61,6 +61,8 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
[self addObserver:self forKeyPath:@"status" options:0 context:kSocketStatusObservationContext];
OWSSingletonAssert();
return self;
}

@ -16,6 +16,8 @@ extern NSString *const TSUIDatabaseConnectionDidUpdateNotification;
@interface TSStorageManager : NSObject
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)sharedManager;
/**

@ -129,6 +129,8 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
[NSException raise:TSStorageManagerExceptionNameNoDatabase format:@"Failed to initialize database."];
}
OWSSingletonAssert();
}
return self;

@ -35,3 +35,37 @@ NSCAssert(0, @"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
#endif
#endif
#pragma mark - Singleton Asserts
// The "singleton asserts" can be used to ensure
// that we only create a singleton once.
//
// The simplest way to use them is the OWSSingletonAssert() macro.
// It is intended to be used inside the singleton's initializer.
//
// If, however, a singleton has multiple possible initializers,
// you need to:
//
// 1. Use OWSSingletonAssertFlag() outside the class definition.
// 2. Use OWSSingletonAssertInit() in each initializer.
#ifdef DEBUG
#define OWSSingletonAssertFlag() static BOOL _isSingletonCreated = NO;
#define OWSSingletonAssertInit() \
@synchronized([self class]) \
{ \
OWSAssert(!_isSingletonCreated); \
_isSingletonCreated = YES; \
}
#define OWSSingletonAssert() OWSSingletonAssertFlag() OWSSingletonAssertInit()
#else
#define OWSSingletonAssertFlag()
#define OWSSingletonAssertInit()
#define OWSSingletonAssert()
#endif

Loading…
Cancel
Save