Skip to content

Commit 94a6b1a

Browse files
authored
ref: Remove SentryDefaultObjCRuntimeWrapper singleton pattern and use SentryDependencyContainer (#5670)
* ref: Remove `SentryDefaultObjCRuntimeWrapper` singleton pattern and use `SentryDependencyContainer` * Update broken tests
1 parent 65ebbdb commit 94a6b1a

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

Sources/Sentry/SentryDefaultObjCRuntimeWrapper.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33

44
@implementation SentryDefaultObjCRuntimeWrapper
55

6-
+ (instancetype)sharedInstance
7-
{
8-
static SentryDefaultObjCRuntimeWrapper *instance = nil;
9-
static dispatch_once_t onceToken;
10-
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
11-
return instance;
12-
}
13-
146
- (const char **)copyClassNamesForImage:(const char *)image amount:(unsigned int *)outCount
157
{
168
return objc_copyClassNamesForImage(image, outCount);

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#import "SentryApplication.h"
44
#import "SentryBinaryImageCache.h"
5+
#import "SentryDefaultObjCRuntimeWrapper.h"
56
#import "SentryDispatchFactory.h"
67
#import "SentryDisplayLinkWrapper.h"
78
#import "SentryExtraContextProvider.h"
@@ -451,4 +452,9 @@ - (SentrySessionTracker *)getSessionTrackerWithOptions:(SentryOptions *)options
451452
dateProvider:self.dateProvider
452453
notificationCenter:self.notificationCenterWrapper];
453454
}
455+
456+
- (id<SentryObjCRuntimeWrapper>)objcRuntimeWrapper SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
457+
{
458+
SENTRY_LAZY_INIT(_objcRuntimeWrapper, [[SentryDefaultObjCRuntimeWrapper alloc] init]);
459+
}
454460
@end

Sources/Sentry/SentryDependencyContainerSwiftHelper.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ + (void)dispatchSyncOnMainQueue:(void (^)(void))block
1919
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchSyncOnMainQueue:block];
2020
}
2121

22+
+ (id<SentryObjCRuntimeWrapper>)objcRuntimeWrapper
23+
{
24+
return SentryDependencyContainer.sharedInstance.objcRuntimeWrapper;
25+
}
26+
2227
@end

Sources/Sentry/SentryPerformanceTrackingIntegration.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#if SENTRY_HAS_UIKIT
44

5-
# import "SentryDefaultObjCRuntimeWrapper.h"
65
# import "SentryDependencyContainer.h"
76
# import "SentryLogC.h"
87
# import "SentryNSProcessInfoWrapper.h"
@@ -34,13 +33,13 @@ - (BOOL)installWithOptions:(SentryOptions *)options
3433

3534
SentrySubClassFinder *subClassFinder = [[SentrySubClassFinder alloc]
3635
initWithDispatchQueue:dispatchQueue
37-
objcRuntimeWrapper:[SentryDefaultObjCRuntimeWrapper sharedInstance]
36+
objcRuntimeWrapper:[SentryDependencyContainer.sharedInstance objcRuntimeWrapper]
3837
swizzleClassNameExcludes:options.swizzleClassNameExcludes];
3938

4039
self.swizzling = [[SentryUIViewControllerSwizzling alloc]
4140
initWithOptions:options
4241
dispatchQueue:dispatchQueue
43-
objcRuntimeWrapper:[SentryDefaultObjCRuntimeWrapper sharedInstance]
42+
objcRuntimeWrapper:[SentryDependencyContainer.sharedInstance objcRuntimeWrapper]
4443
subClassFinder:subClassFinder
4544
processInfoWrapper:[SentryDependencyContainer.sharedInstance processInfoWrapper]
4645
binaryImageCache:[SentryDependencyContainer.sharedInstance binaryImageCache]];

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@protocol SentryApplication;
3434
@protocol SentryDispatchQueueProviderProtocol;
3535
@protocol SentryNSNotificationCenterWrapper;
36+
@protocol SentryObjCRuntimeWrapper;
3637

3738
#if SENTRY_HAS_METRIC_KIT
3839
@class SentryMXManager;
@@ -135,6 +136,7 @@ SENTRY_NO_INIT
135136
@property (nonatomic, strong) SentryMXManager *metricKitManager API_AVAILABLE(
136137
ios(15.0), macos(12.0), macCatalyst(15.0)) API_UNAVAILABLE(tvos, watchos);
137138
#endif // SENTRY_HAS_METRIC_KIT
139+
@property (nonatomic, strong) id<SentryObjCRuntimeWrapper> objcRuntimeWrapper;
138140

139141
#if SENTRY_HAS_UIKIT
140142
- (SentryWatchdogTerminationScopeObserver *)getWatchdogTerminationScopeObserverWithOptions:

Sources/Sentry/include/SentryDefaultObjCRuntimeWrapper.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55
* A wrapper around the objc runtime functions for testability.
66
*/
77
@interface SentryDefaultObjCRuntimeWrapper : NSObject <SentryObjCRuntimeWrapper>
8-
SENTRY_NO_INIT
9-
10-
+ (instancetype)sharedInstance;
118

129
@end

Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# import <UIKit/UIKit.h>
66
#endif // SENTRY_HAS_UIKIT
77

8+
@protocol SentryObjCRuntimeWrapper;
9+
810
NS_ASSUME_NONNULL_BEGIN
911

1012
// Some Swift code needs to access SentryDependencyContainer. To
@@ -20,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
2022
#endif // SENTRY_HAS_UIKIT
2123

2224
+ (void)dispatchSyncOnMainQueue:(void (^)(void))block;
25+
+ (id<SentryObjCRuntimeWrapper>)objcRuntimeWrapper;
2326

2427
@end
2528

Tests/SentryTests/Helper/SentryTestObjCRuntimeWrapper.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#import "SentryTestObjCRuntimeWrapper.h"
2-
#import "SentryDefaultObjCRuntimeWrapper.h"
2+
#import "SentryDependencyContainer.h"
3+
#import "SentryObjCRuntimeWrapper.h"
34
#import <objc/runtime.h>
45

56
@interface SentryTestObjCRuntimeWrapper ()
67

7-
@property (nonatomic, strong) SentryDefaultObjCRuntimeWrapper *objcRuntimeWrapper;
8+
@property (nonatomic, strong) id<SentryObjCRuntimeWrapper> objcRuntimeWrapper;
89

910
@end
1011

@@ -13,7 +14,7 @@ @implementation SentryTestObjCRuntimeWrapper
1314
- (instancetype)init
1415
{
1516
if (self = [super init]) {
16-
self.objcRuntimeWrapper = [SentryDefaultObjCRuntimeWrapper sharedInstance];
17+
self.objcRuntimeWrapper = [[SentryDependencyContainer sharedInstance] objcRuntimeWrapper];
1718
}
1819

1920
return self;

Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerSwizzlingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SentryUIViewControllerSwizzlingTests: XCTestCase {
3232
}
3333

3434
var sutWithDefaultObjCRuntimeWrapper: SentryUIViewControllerSwizzling {
35-
return SentryUIViewControllerSwizzling(options: options, dispatchQueue: dispatchQueue, objcRuntimeWrapper: SentryDefaultObjCRuntimeWrapper.sharedInstance(), subClassFinder: subClassFinder, processInfoWrapper: processInfoWrapper, binaryImageCache: binaryImageCache)
35+
return SentryUIViewControllerSwizzling(options: options, dispatchQueue: dispatchQueue, objcRuntimeWrapper: SentryDependencyContainerSwiftHelper.objcRuntimeWrapper(), subClassFinder: subClassFinder, processInfoWrapper: processInfoWrapper, binaryImageCache: binaryImageCache)
3636
}
3737

3838
var testableSut: TestSentryUIViewControllerSwizzling {

0 commit comments

Comments
 (0)