Skip to content

Commit 1f48bb7

Browse files
authored
ref: Use a callback for SentryFileManager (#6171)
1 parent e61a572 commit 1f48bb7

File tree

7 files changed

+26
-46
lines changed

7 files changed

+26
-46
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@
536536
7BD4BD4127EB0F0D0071F4FF /* SentryDiscardReason.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BD4BD4027EB0F0C0071F4FF /* SentryDiscardReason.h */; };
537537
7BD4BD4B27EB2DC20071F4FF /* SentryDiscardedEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD4BD4A27EB2DC20071F4FF /* SentryDiscardedEventTests.swift */; };
538538
7BD4BD4D27EB31820071F4FF /* SentryClientReportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD4BD4C27EB31820071F4FF /* SentryClientReportTests.swift */; };
539-
7BD4E8E627FD84480086C410 /* TestFileManagerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD4E8E527FD84480086C410 /* TestFileManagerDelegate.swift */; };
540539
7BD4E8E827FD95900086C410 /* SentryMigrateSessionInitTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BD4E8E727FD95900086C410 /* SentryMigrateSessionInitTests.m */; };
541540
7BD729962463E83300EA3610 /* SentryDateUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BD729952463E83300EA3610 /* SentryDateUtil.h */; };
542541
7BD729982463E93500EA3610 /* SentryDateUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BD729972463E93500EA3610 /* SentryDateUtil.m */; };
@@ -1813,7 +1812,6 @@
18131812
7BD4BD4027EB0F0C0071F4FF /* SentryDiscardReason.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDiscardReason.h; path = include/SentryDiscardReason.h; sourceTree = "<group>"; };
18141813
7BD4BD4A27EB2DC20071F4FF /* SentryDiscardedEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDiscardedEventTests.swift; sourceTree = "<group>"; };
18151814
7BD4BD4C27EB31820071F4FF /* SentryClientReportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryClientReportTests.swift; sourceTree = "<group>"; };
1816-
7BD4E8E527FD84480086C410 /* TestFileManagerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestFileManagerDelegate.swift; sourceTree = "<group>"; };
18171815
7BD4E8E727FD95900086C410 /* SentryMigrateSessionInitTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryMigrateSessionInitTests.m; sourceTree = "<group>"; };
18181816
7BD729952463E83300EA3610 /* SentryDateUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDateUtil.h; path = include/SentryDateUtil.h; sourceTree = "<group>"; };
18191817
7BD729972463E93500EA3610 /* SentryDateUtil.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryDateUtil.m; sourceTree = "<group>"; };
@@ -3587,7 +3585,6 @@
35873585
7BBD18BA24530D2600427C76 /* SentryFileManagerTests.swift */,
35883586
FA21A2E92E60E9C700E7EADB /* EnvelopeComparison.swift */,
35893587
7BD4E8E727FD95900086C410 /* SentryMigrateSessionInitTests.m */,
3590-
7BD4E8E527FD84480086C410 /* TestFileManagerDelegate.swift */,
35913588
631501BA1EE6F30B00512C5B /* SentrySwizzleTests.m */,
35923589
7B34721628086A9D0041F047 /* SentrySwizzleWrapperTests.swift */,
35933590
D808FB89281BCE46009A2A33 /* TestSentrySwizzleWrapper.swift */,
@@ -6019,7 +6016,6 @@
60196016
631501BB1EE6F30B00512C5B /* SentrySwizzleTests.m in Sources */,
60206017
15D0AC882459EE4D006541C2 /* SentryNSURLRequestTests.swift in Sources */,
60216018
7BE3C78724472E9800A38442 /* TestRequestManager.swift in Sources */,
6022-
7BD4E8E627FD84480086C410 /* TestFileManagerDelegate.swift in Sources */,
60236019
63FE722220DA66EC00CDBAE8 /* SentryCrashJSONCodec_Tests.m in Sources */,
60246020
7B0A5452252311CE00A71716 /* SentryBreadcrumbTests.swift in Sources */,
60256021
7BE3C7752445C82300A38442 /* SentryCurrentDateTests.swift in Sources */,

Sources/Sentry/SentryFileManager.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ @interface SentryFileManager ()
111111
@property (nonatomic, copy) NSString *appHangEventFilePath;
112112
@property (nonatomic, assign) NSUInteger currentFileCounter;
113113
@property (nonatomic, assign) NSUInteger maxEnvelopes;
114-
@property (nonatomic, weak) id<SentryFileManagerDelegate> delegate;
114+
@property (nonatomic, copy, nullable) void (^envelopeDeletedCallback)
115+
(SentryEnvelopeItem *, SentryDataCategory);
115116

116117
@end
117118

@@ -200,9 +201,10 @@ - (void)createPathsWithOptions:(SentryOptions *)options
200201
self.envelopesPath = [self.sentryPath stringByAppendingPathComponent:EnvelopesPathComponent];
201202
}
202203

203-
- (void)setDelegate:(id<SentryFileManagerDelegate>)delegate
204+
- (void)setEnvelopeDeletedCallback:(void (^)(
205+
SentryEnvelopeItem *_Nonnull, SentryDataCategory))callback
204206
{
205-
_delegate = delegate;
207+
_envelopeDeletedCallback = callback;
206208
}
207209

208210
#pragma mark - Convenience Accessors
@@ -1089,7 +1091,9 @@ - (void)handleEnvelopesLimit
10891091
continue;
10901092
}
10911093

1092-
[_delegate envelopeItemDeleted:item withCategory:rateLimitCategory];
1094+
if (self.envelopeDeletedCallback) {
1095+
self.envelopeDeletedCallback(item, rateLimitCategory);
1096+
}
10931097
}
10941098

10951099
[self removeFileAtPath:envelopeFilePath];

Sources/Sentry/SentryHttpTransport.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ - (id)initWithDsn:(SentryDsn *)dsn
9191
self.discardedEvents = [NSMutableDictionary new];
9292
self.notStoredEnvelopes = [NSMutableArray new];
9393
[self.envelopeRateLimit setDelegate:self];
94-
[self.fileManager setDelegate:self];
94+
typeof(self) __weak weakSelf = self;
95+
[self.fileManager
96+
setEnvelopeDeletedCallback:^(SentryEnvelopeItem *item, SentryDataCategory category) {
97+
[weakSelf envelopeItemDeleted:item withCategory:category];
98+
}];
9599

96100
[self sendAllCachedEnvelopes];
97101

@@ -260,9 +264,6 @@ - (void)envelopeItemDropped:(SentryEnvelopeItem *)envelopeItem
260264
[self recordLostSpans:envelopeItem reason:kSentryDiscardReasonRateLimitBackoff];
261265
}
262266

263-
/**
264-
* SentryFileManagerDelegate.
265-
*/
266267
- (void)envelopeItemDeleted:(SentryEnvelopeItem *)envelopeItem
267268
withCategory:(SentryDataCategory)dataCategory
268269
{

Sources/Sentry/include/SentryFileManager.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
NS_ASSUME_NONNULL_BEGIN
66

7-
@protocol SentryFileManagerDelegate;
8-
97
@class SentryAppState;
108
@class SentryDispatchQueueWrapper;
119
@class SentryEvent;
@@ -16,12 +14,6 @@ NS_ASSUME_NONNULL_BEGIN
1614
@class SentrySession;
1715

1816
@protocol SentryCurrentDateProvider;
19-
@protocol SentryFileManagerDelegate <NSObject>
20-
21-
- (void)envelopeItemDeleted:(SentryEnvelopeItem *)envelopeItem
22-
withCategory:(SentryDataCategory)dataCategory;
23-
24-
@end
2517

2618
NS_SWIFT_NAME(SentryFileManager)
2719
@interface SentryFileManager : NSObject
@@ -40,7 +32,7 @@ SENTRY_NO_INIT
4032
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
4133
error:(NSError **)error NS_DESIGNATED_INITIALIZER;
4234

43-
- (void)setDelegate:(id<SentryFileManagerDelegate>)delegate;
35+
- (void)setEnvelopeDeletedCallback:(void (^)(SentryEnvelopeItem *, SentryDataCategory))callback;
4436

4537
#pragma mark - Envelope
4638

Sources/Sentry/include/SentryHttpTransport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
NS_ASSUME_NONNULL_BEGIN
1414

15-
@interface SentryHttpTransport
16-
: NSObject <SentryTransport, SentryEnvelopeRateLimitDelegate, SentryFileManagerDelegate>
15+
@interface SentryHttpTransport : NSObject <SentryTransport, SentryEnvelopeRateLimitDelegate>
1716
SENTRY_NO_INIT
1817

1918
- (id)initWithDsn:(SentryDsn *)dsn

Tests/SentryTests/Helper/SentryFileManagerTests.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@ class SentryFileManagerTests: XCTestCase {
1919
let session = SentrySession(releaseName: "1.0.0", distinctId: "some-id")
2020
let sessionEnvelope: SentryEnvelope
2121

22+
var envelopeItemsDeleted = Invocations<SentryDataCategory>()
23+
2224
let sessionUpdate: SentrySession
2325
let sessionUpdateEnvelope: SentryEnvelope
2426

2527
let expectedSessionUpdate: SentrySession
2628

27-
// swiftlint:disable weak_delegate
28-
// Swiftlint automatically changes this to a weak reference,
29-
// but we need a strong reference to make the test work.
30-
var delegate: TestFileManagerDelegate!
31-
// swiftlint:enable weak_delegate
32-
3329
@available(*, deprecated, message: "This is only marked as deprecated because enableAppLaunchProfiling is marked as deprecated. Once that is removed this can be removed.")
3430
init() throws {
3531
currentDateProvider = TestCurrentDateProvider()
@@ -56,8 +52,6 @@ class SentryFileManagerTests: XCTestCase {
5652
expectedSessionUpdate = SentrySession(jsonObject: sessionUpdateCopy.serialize())!
5753
// We can only set the init flag after serialize, because the duration is not set if the init flag is set
5854
expectedSessionUpdate.setFlagInit()
59-
60-
delegate = TestFileManagerDelegate()
6155
}
6256

6357
func getSut() -> SentryFileManager {
@@ -66,7 +60,9 @@ class SentryFileManagerTests: XCTestCase {
6660
dateProvider: currentDateProvider,
6761
dispatchQueueWrapper: dispatchQueueWrapper
6862
)
69-
sut.setDelegate(delegate)
63+
sut.setEnvelopeDeletedCallback { [weak self] _, category in
64+
self?.envelopeItemsDeleted.record(category)
65+
}
7066
return sut
7167
}
7268

@@ -77,7 +73,9 @@ class SentryFileManagerTests: XCTestCase {
7773
dateProvider: currentDateProvider,
7874
dispatchQueueWrapper: dispatchQueueWrapper
7975
)
80-
sut.setDelegate(delegate)
76+
sut.setEnvelopeDeletedCallback { [weak self] _, category in
77+
self?.envelopeItemsDeleted.record(category)
78+
}
8179
return sut
8280
}
8381

@@ -359,9 +357,9 @@ class SentryFileManagerTests: XCTestCase {
359357
sut.store(TestConstants.envelope)
360358
}
361359

362-
XCTAssertEqual(4, fixture.delegate.envelopeItemsDeleted.count)
360+
XCTAssertEqual(4, fixture.envelopeItemsDeleted.count)
363361
let expected: [SentryDataCategory] = [.error, .attachment, .session, .error]
364-
XCTAssertEqual(expected, fixture.delegate.envelopeItemsDeleted.invocations)
362+
XCTAssertEqual(expected, fixture.envelopeItemsDeleted.invocations)
365363
}
366364

367365
@available(*, deprecated, message: "This is only marked as deprecated because enableAppLaunchProfiling is marked as deprecated. Once that is removed this can be removed.")
@@ -499,7 +497,7 @@ class SentryFileManagerTests: XCTestCase {
499497
sut.store(TestConstants.envelope)
500498
}
501499

502-
XCTAssertEqual(0, fixture.delegate.envelopeItemsDeleted.count)
500+
XCTAssertEqual(0, fixture.envelopeItemsDeleted.count)
503501
}
504502

505503
@available(*, deprecated, message: "This is only marked as deprecated because enableAppLaunchProfiling is marked as deprecated. Once that is removed this can be removed.")

Tests/SentryTests/Helper/TestFileManagerDelegate.swift

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

0 commit comments

Comments
 (0)