Skip to content

Commit 62d9cf0

Browse files
authored
refactor: Convert SentryCoreDataTrackingIntegration to Swift (#7255)
* chore: Convert `SentryNSDataSwizzling` and `SentryNSFileManagerSwizzling` to Swift * refactor: Remove legacy swizzling imports and add new helper classes for swizzling functionality * Add tests for SentryNSDataSwizzlingHelper * Add SentryNSFileManagerSwizzlingHelper and corresponding tests * Use weak reference for tracker * refactor: remove unused tracker property from SentryNSDataSwizzling and SentryNSFileManagerSwizzling * Rename tests * refactor: Convert `SentryCoreDataTrackingIntegration` to Swift * Move fetchManagedObjectContext helper method to SentryCoreDataTrackerTests * Remove singleton * Fix build on macOS * Hold a local reference for the tracker * Fixes in project.pbxproj * Fix build on non UIKit builds * Make `unswizzle` only available for tests * Update headers * Fix duplicate file * Enable `unswizzle` method only in test environments * Fix integration stop clearing tracker * PR feedback
1 parent 4b41eee commit 62d9cf0

21 files changed

Lines changed: 547 additions & 278 deletions

Sentry.xcodeproj/project.pbxproj

Lines changed: 24 additions & 36 deletions
Large diffs are not rendered by default.

Sources/Sentry/SentryBaseIntegration.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ - (BOOL)shouldBeEnabledWithOptions:(SentryOptions *)options
8585
return NO;
8686
}
8787

88-
if ((integrationOptions & kIntegrationOptionEnableCoreDataTracing)
89-
&& !options.enableCoreDataTracing) {
90-
[self logWithOptionName:@"enableCoreDataTracing"];
91-
return NO;
92-
}
93-
9488
if ((integrationOptions & kIntegrationOptionEnableSwizzling) && !options.enableSwizzling) {
9589
[self logWithOptionName:@"enableSwizzling"];
9690
return NO;

Sources/Sentry/SentryCoreDataSwizzling.m

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#import "SentryCoreDataSwizzlingHelper.h"
2+
#import "SentryCoreDataTracker.h"
3+
#import "SentrySwift.h"
4+
#import "SentrySwizzle.h"
5+
#import <CoreData/CoreData.h>
6+
#import <objc/runtime.h>
7+
8+
@implementation SentryCoreDataSwizzlingHelper
9+
10+
static __weak SentryCoreDataTracker *_tracker = nil;
11+
#if SENTRY_TEST || SENTRY_TEST_CI
12+
static BOOL swizzlingIsActive = FALSE;
13+
#endif
14+
15+
// SentrySwizzleInstanceMethod declaration shadows a local variable. The swizzling is working
16+
// fine and we accept this warning.
17+
#pragma clang diagnostic push
18+
#pragma clang diagnostic ignored "-Wshadow"
19+
+ (void)swizzleWithTracker:(SentryCoreDataTracker *)tracker
20+
{
21+
_tracker = tracker;
22+
#if SENTRY_TEST || SENTRY_TEST_CI
23+
swizzlingIsActive = TRUE;
24+
#endif
25+
26+
SEL fetchSelector = NSSelectorFromString(@"executeFetchRequest:error:");
27+
SentrySwizzleInstanceMethod(NSManagedObjectContext.class, fetchSelector,
28+
SentrySWReturnType(NSArray *),
29+
SentrySWArguments(NSFetchRequest * originalRequest, NSError * *error), SentrySWReplacement({
30+
SentryCoreDataTracker *tracker = _tracker;
31+
return tracker != nil
32+
? [tracker
33+
managedObjectContext:self
34+
executeFetchRequest:originalRequest
35+
error:error
36+
originalImp:^NSArray *(NSFetchRequest *request, NSError **outError) {
37+
return SentrySWCallOriginal(request, outError);
38+
}]
39+
: SentrySWCallOriginal(originalRequest, error);
40+
}),
41+
SentrySwizzleModeOncePerClassAndSuperclasses, (void *)fetchSelector);
42+
43+
SEL saveSelector = NSSelectorFromString(@"save:");
44+
SentrySwizzleInstanceMethod(NSManagedObjectContext.class, saveSelector,
45+
SentrySWReturnType(BOOL), SentrySWArguments(NSError * *error), SentrySWReplacement({
46+
SentryCoreDataTracker *tracker = _tracker;
47+
return tracker != nil ? [tracker managedObjectContext:self
48+
save:error
49+
originalImp:^BOOL(NSError **outError) {
50+
return SentrySWCallOriginal(outError);
51+
}]
52+
: SentrySWCallOriginal(error);
53+
}),
54+
SentrySwizzleModeOncePerClassAndSuperclasses, (void *)saveSelector);
55+
}
56+
57+
+ (void)stop
58+
{
59+
_tracker = nil;
60+
#if SENTRY_TEST || SENTRY_TEST_CI
61+
[self unswizzle];
62+
#endif
63+
}
64+
65+
#if SENTRY_TEST || SENTRY_TEST_CI
66+
+ (void)unswizzle
67+
{
68+
swizzlingIsActive = FALSE;
69+
70+
// Unswizzling is only supported in test targets as it is considered unsafe for production.
71+
SEL fetchSelector = NSSelectorFromString(@"executeFetchRequest:error:");
72+
SentryUnswizzleInstanceMethod(
73+
NSManagedObjectContext.class, fetchSelector, (void *)fetchSelector);
74+
75+
SEL saveSelector = NSSelectorFromString(@"save:");
76+
SentryUnswizzleInstanceMethod(NSManagedObjectContext.class, saveSelector, (void *)saveSelector);
77+
}
78+
# pragma clang diagnostic pop
79+
80+
+ (BOOL)swizzlingActive
81+
{
82+
return swizzlingIsActive;
83+
}
84+
#endif
85+
@end

Sources/Sentry/SentryCoreDataTrackingIntegration.m

Lines changed: 0 additions & 41 deletions
This file was deleted.

Sources/Sentry/SentrySDKInternal.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#import "SentryAppStartMeasurement.h"
44
#import "SentryBreadcrumb.h"
55
#import "SentryClient+Private.h"
6-
#import "SentryCoreDataTrackingIntegration.h"
76
#import "SentryCrash.h"
87
#import "SentryHub+Private.h"
8+
#import "SentryIntegrationProtocol.h"
99
#import "SentryInternalDefines.h"
1010
#import "SentryLogC.h"
1111
#import "SentryMeta.h"
@@ -495,14 +495,15 @@ + (void)endSession
495495

496496
+ (NSArray<Class> *)defaultIntegrationClasses
497497
{
498-
// The order of integrations here is important.
499-
NSMutableArray<Class> *defaultIntegrations = [NSMutableArray<Class> arrayWithObjects:
500498
#if SENTRY_HAS_UIKIT
501-
[SentryAppStartTrackingIntegration class], [SentryPerformanceTrackingIntegration class],
502-
#endif // SENTRY_HAS_UIKIT
503-
[SentryCoreDataTrackingIntegration class], nil];
504-
499+
// The order of integrations here is important.
500+
NSMutableArray<Class> *defaultIntegrations =
501+
[NSMutableArray<Class> arrayWithObjects:[SentryAppStartTrackingIntegration class],
502+
[SentryPerformanceTrackingIntegration class], nil];
505503
return defaultIntegrations;
504+
#else
505+
return @[];
506+
#endif // SENTRY_HAS_UIKIT
506507
}
507508

508509
/**

Sources/Sentry/include/SentryCoreDataSwizzling.h

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import "SentryDefines.h"
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
@interface SentryCoreDataSwizzlingHelper : NSObject
6+
7+
+ (void)swizzleWithTracker:(SENTRY_SWIFT_MIGRATION_ID(SentryCoreDataTracker))tracker;
8+
9+
+ (void)stop;
10+
11+
#if SENTRY_TEST || SENTRY_TEST_CI
12+
+ (BOOL)swizzlingActive;
13+
#endif
14+
15+
@end
16+
17+
NS_ASSUME_NONNULL_END

Sources/Sentry/include/SentryCoreDataTracker.h

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

44
NS_ASSUME_NONNULL_BEGIN
55

6-
@protocol SentryProcessInfoSource;
76
@class SentryDefaultThreadInspector;
87

98
@interface SentryCoreDataTracker : NSObject
109
SENTRY_NO_INIT
1110

1211
- (instancetype)initWithThreadInspector:(SentryDefaultThreadInspector *)threadInspector
13-
processInfoWrapper:(id<SentryProcessInfoSource>)processInfoWrapper;
12+
processInfoWrapper:
13+
(SENTRY_SWIFT_MIGRATION_ID(id<SentryProcessInfoSource>))processInfoWrapper;
1414

1515
- (NSArray *)managedObjectContext:(NSManagedObjectContext *)context
1616
executeFetchRequest:(NSFetchRequest *)request

Sources/Sentry/include/SentryCoreDataTrackingIntegration.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)