From c0f1495b0e260673942244eec7805b00dd0d6650 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Fri, 18 Aug 2023 12:46:16 -0800 Subject: [PATCH] ref: report viewDidAppear breadcrumbs via delegate --- Sources/Sentry/SentryBreadcrumbTracker.m | 17 +++++++-------- .../SentryBreadcrumbTrackerTests.swift | 21 ++++++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Sources/Sentry/SentryBreadcrumbTracker.m b/Sources/Sentry/SentryBreadcrumbTracker.m index f2cccb57d7d..6190e90a671 100644 --- a/Sources/Sentry/SentryBreadcrumbTracker.m +++ b/Sources/Sentry/SentryBreadcrumbTracker.m @@ -5,7 +5,6 @@ #import "SentryDefines.h" #import "SentryHub.h" #import "SentryLog.h" -#import "SentrySDK+Private.h" #import "SentryScope.h" #import "SentrySwift.h" #import "SentrySwizzle.h" @@ -201,17 +200,15 @@ - (void)swizzleViewDidAppear static const void *swizzleViewDidAppearKey = &swizzleViewDidAppearKey; SEL selector = NSSelectorFromString(@"viewDidAppear:"); + __weak id delegate = self.delegate; SentrySwizzleInstanceMethod(UIViewController.class, selector, SentrySWReturnType(void), SentrySWArguments(BOOL animated), SentrySWReplacement({ - if (nil != [SentrySDK.currentHub getClient]) { - SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo - category:@"ui.lifecycle"]; - crumb.type = @"navigation"; - crumb.data = [SentryBreadcrumbTracker fetchInfoAboutViewController:self]; - - // Adding crumb via the SDK calls SentryBeforeBreadcrumbCallback - [SentrySDK addBreadcrumb:crumb]; - } + SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo + category:@"ui.lifecycle"]; + crumb.type = @"navigation"; + crumb.data = [SentryBreadcrumbTracker fetchInfoAboutViewController:self]; + [delegate addBreadcrumb:crumb]; + SentrySWCallOriginal(animated); }), SentrySwizzleModeOncePerClassAndSuperclasses, swizzleViewDidAppearKey); diff --git a/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift b/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift index 655c14aba69..212fd1a31f9 100644 --- a/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift @@ -45,18 +45,19 @@ class SentryBreadcrumbTrackerTests: XCTestCase { viewController.title = "test title" viewController.viewDidAppear(false) - let crumbs = Dynamic(scope).breadcrumbArray.asArray as? [Breadcrumb] + let crumbs = delegate.addCrumbInvocations.invocations - XCTAssertEqual(1, crumbs?.count) + // one breadcrumb for starting the tracker, and a second one for the swizzled viewDidAppear + XCTAssertEqual(2, crumbs.count) - let lifeCycleCrumb = crumbs?[0] - XCTAssertEqual("navigation", lifeCycleCrumb?.type) - XCTAssertEqual("ui.lifecycle", lifeCycleCrumb?.category) - XCTAssertEqual("false", lifeCycleCrumb?.data?["beingPresented"] as? String) - XCTAssertEqual("UIViewController", lifeCycleCrumb?.data?["screen"] as? String) - XCTAssertEqual("test title", lifeCycleCrumb?.data?["title"] as? String) - XCTAssertEqual("false", lifeCycleCrumb?.data?["beingPresented"] as? String) - XCTAssertEqual("UINavigationController", lifeCycleCrumb?.data?["parentViewController"] as? String) + let lifeCycleCrumb = crumbs[1] + XCTAssertEqual("navigation", lifeCycleCrumb.type) + XCTAssertEqual("ui.lifecycle", lifeCycleCrumb.category) + XCTAssertEqual("false", lifeCycleCrumb.data?["beingPresented"] as? String) + XCTAssertEqual("UIViewController", lifeCycleCrumb.data?["screen"] as? String) + XCTAssertEqual("test title", lifeCycleCrumb.data?["title"] as? String) + XCTAssertEqual("false", lifeCycleCrumb.data?["beingPresented"] as? String) + XCTAssertEqual("UINavigationController", lifeCycleCrumb.data?["parentViewController"] as? String) clearTestState() }