Skip to content

Commit

Permalink
ref: Reduce complexity for SentryDependencyContainer (#3294)
Browse files Browse the repository at this point in the history
Making SentryDependencyContainer less complex and more predictable by removing some unnecessary lazy loads.

The goal would be to have all necessary objects initialized according to SentryOptions and remove all the @syncronized locks.
  • Loading branch information
brustolin authored Sep 25, 2023
1 parent 898177b commit 324dc7b
Showing 1 changed file with 15 additions and 79 deletions.
94 changes: 15 additions & 79 deletions Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,31 @@ + (void)initialize
{
if (self == [SentryDependencyContainer class]) {
sentryDependencyContainerLock = [[NSObject alloc] init];
instance = [[SentryDependencyContainer alloc] init];
}
}

+ (instancetype)sharedInstance
{
@synchronized(sentryDependencyContainerLock) {
if (instance == nil) {
instance = [[self alloc] init];
}
return instance;
}
return instance;
}

+ (void)reset
{
@synchronized(sentryDependencyContainerLock) {
instance = nil;
instance = [[SentryDependencyContainer alloc] init];
}

- (instancetype)init
{
if (self = [super init]) {
_dispatchQueueWrapper = [[SentryDispatchQueueWrapper alloc] init];
_random = [[SentryRandom alloc] init];
_threadWrapper = [[SentryThreadWrapper alloc] init];
_binaryImageCache = [[SentryBinaryImageCache alloc] init];
_debugImageProvider = [[SentryDebugImageProvider alloc] init];
_dateProvider = [[SentryCurrentDateProvider alloc] init];
}
return self;
}

- (SentryFileManager *)fileManager
Expand Down Expand Up @@ -130,28 +137,6 @@ - (SentryExtraContextProvider *)extraContextProvider
return _extraContextProvider;
}

- (SentryThreadWrapper *)threadWrapper
{
if (_threadWrapper == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_threadWrapper == nil) {
_threadWrapper = [[SentryThreadWrapper alloc] init];
}
}
}
return _threadWrapper;
}

- (SentryDispatchQueueWrapper *)dispatchQueueWrapper
{
@synchronized(sentryDependencyContainerLock) {
if (_dispatchQueueWrapper == nil) {
_dispatchQueueWrapper = [[SentryDispatchQueueWrapper alloc] init];
}
return _dispatchQueueWrapper;
}
}

- (SentryNSNotificationCenterWrapper *)notificationCenterWrapper
{
@synchronized(sentryDependencyContainerLock) {
Expand All @@ -162,30 +147,6 @@ - (SentryNSNotificationCenterWrapper *)notificationCenterWrapper
}
}

- (id<SentryRandom>)random
{
if (_random == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_random == nil) {
_random = [[SentryRandom alloc] init];
}
}
}
return _random;
}

- (SentryBinaryImageCache *)binaryImageCache
{
if (_binaryImageCache == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_binaryImageCache == nil) {
_binaryImageCache = [[SentryBinaryImageCache alloc] init];
}
}
}
return _binaryImageCache;
}

#if TARGET_OS_IOS
- (SentryUIDeviceWrapper *)uiDeviceWrapper
{
Expand Down Expand Up @@ -263,19 +224,6 @@ - (SentrySwizzleWrapper *)swizzleWrapper
return _swizzleWrapper;
}

- (SentryDebugImageProvider *)debugImageProvider
{
if (_debugImageProvider == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_debugImageProvider == nil) {
_debugImageProvider = [[SentryDebugImageProvider alloc] init];
}
}
}

return _debugImageProvider;
}

- (SentryANRTracker *)getANRTracker:(NSTimeInterval)timeout
{
if (_anrTracker == nil) {
Expand Down Expand Up @@ -341,18 +289,6 @@ - (SentryNSTimerFactory *)timerFactory
return _timerFactory;
}

- (SentryCurrentDateProvider *)dateProvider
{
if (_dateProvider == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_dateProvider == nil) {
_dateProvider = [[SentryCurrentDateProvider alloc] init];
}
}
}
return _dateProvider;
}

#if SENTRY_HAS_METRIC_KIT
- (SentryMXManager *)metricKitManager
{
Expand Down

0 comments on commit 324dc7b

Please sign in to comment.