Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-1218 App going background stops active RUM view #479

Merged
merged 3 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ internal class UIKitRUMViewsHandler: UIKitRUMViewsHandlerType {
self.predicate = predicate
self.dateProvider = dateProvider
self.inspector = inspector

let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
nc.addObserver(self, selector: #selector(appDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}

private weak var lastActiveAppVC: UIViewController?
@objc
private func appWillResignActive() {
if lastActiveAppVC != nil {
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the lastActiveAppVC is not nil but doesn't match the lastStartedViewController ? When the app comes back in background, won't you be starting the wrong RUM View ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wondering if we need the lastActiveAppVC property at all. As far as I can see, we either set it to lastStartedViewController or to nil, so can't we simply do:

  • if the app goes to background: stop lastStartedViewController,
  • when it goes to foreground: start lastStartedViewController
    ?

If there's sth special about lastActiveAppVC that I miss, I think it's worth adding it in the property comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xgouchet good point.
now i remove lastActiveAppVC along with @ncreated comments below. it was introduced to guard against multiple notifications received but probably not really needed.

lastActiveAppVC = lastStartedViewController
stop(viewController: lastActiveAppVC)
}

@objc
private func appDidBecomeActive() {
if let vc = lastActiveAppVC,
let rumView = predicate.rumView(for: vc) {
start(viewController: vc, rumView: rumView)
}
lastActiveAppVC = nil
}

// MARK: - UIKitRUMViewsHandlerType
Expand Down Expand Up @@ -81,16 +104,13 @@ internal class UIKitRUMViewsHandler: UIKitRUMViewsHandlerType {
)
}

if let lastStartedViewController = lastStartedViewController {
subscriber?.process(
command: RUMStopViewCommand(
time: dateProvider.currentDate(),
attributes: rumView.attributes,
identity: lastStartedViewController
)
)
}
stop(viewController: lastStartedViewController)
start(viewController: viewController, rumView: rumView)

lastStartedViewController = viewController
}

private func start(viewController: UIViewController, rumView: RUMView) {
subscriber?.process(
command: RUMStartViewCommand(
time: dateProvider.currentDate(),
Expand All @@ -100,7 +120,17 @@ internal class UIKitRUMViewsHandler: UIKitRUMViewsHandlerType {
attributes: rumView.attributes
)
)
}

lastStartedViewController = viewController
private func stop(viewController: UIViewController?) {
if let vc = viewController {
subscriber?.process(
command: RUMStopViewCommand(
time: dateProvider.currentDate(),
attributes: [:],
identity: vc
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class UIKitRUMViewsHandlerTests: XCTestCase {

// Given
predicate.resultByViewController = [
view1: .init(name: .mockRandom()),
view2: .init(name: .mockRandom()),
view1: .init(name: .mockRandom(), attributes: ["key1": "val1"]),
view2: .init(name: .mockRandom(), attributes: ["key2": "val2"]),
]

// When
Expand All @@ -74,8 +74,11 @@ class UIKitRUMViewsHandlerTests: XCTestCase {
let stopCommand = try XCTUnwrap(commandSubscriber.receivedCommands[1] as? RUMStopViewCommand)
let startCommand2 = try XCTUnwrap(commandSubscriber.receivedCommands[2] as? RUMStartViewCommand)
XCTAssertTrue(startCommand1.identity.equals(view1))
XCTAssertEqual(startCommand1.attributes as? [String: String], ["key1": "val1"])
XCTAssertTrue(stopCommand.identity.equals(view1))
XCTAssertEqual(stopCommand.attributes.count, 0)
XCTAssertTrue(startCommand2.identity.equals(view2))
XCTAssertEqual(startCommand2.attributes as? [String: String], ["key2": "val2"])
}

func testGivenAcceptingPredicate_whenViewDidAppear_itDoesNotStartTheSameRUMViewTwice() {
Expand Down Expand Up @@ -193,4 +196,57 @@ class UIKitRUMViewsHandlerTests: XCTestCase {
// Then
XCTAssertEqual(predicate.numberOfCalls, 1)
}

func testGivenRUMViewStarted_whenAppStateChanges_itStopsAndRestartsRUMView() throws {
let viewName: String = .mockRandom()
let viewControllerClassName: String = .mockRandom()
let view = createMockView(viewControllerClassName: viewControllerClassName)

// Given
predicate.result = .init(name: viewName, attributes: ["foo": "bar"])
handler.notify_viewDidAppear(viewController: view, animated: .mockAny())

// When
for _ in 0..<3 {
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
}
dateProvider.advance(bySeconds: 1)
for _ in 0..<3 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's quite unclear why we call it 3 times - I guess it's just for sanity, no? Maybe it's worth expressing somehow (either with an inline comment or by labelling 3 with a named variable).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed ✅

NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
}

// Then
XCTAssertEqual(commandSubscriber.receivedCommands.count, 3)

let stopCommand = try XCTUnwrap(commandSubscriber.receivedCommands[1] as? RUMStopViewCommand)
XCTAssertTrue(stopCommand.identity.equals(view))
XCTAssertEqual(stopCommand.attributes.count, 0)
XCTAssertEqual(stopCommand.time, .mockDecember15th2019At10AMUTC())

let startCommand = try XCTUnwrap(commandSubscriber.receivedCommands[2] as? RUMStartViewCommand)
XCTAssertTrue(startCommand.identity.equals(view))
XCTAssertEqual(startCommand.path, viewControllerClassName)
XCTAssertEqual(startCommand.name, viewName)
XCTAssertEqual(startCommand.attributes as? [String: String], ["foo": "bar"])
XCTAssertEqual(startCommand.time, .mockDecember15th2019At10AMUTC() + 1)
}

func testGivenRUMViewDidNotStart_whenAppStateChanges_itDoesNothing() throws {
let viewName: String = .mockRandom()

// Given
predicate.result = .init(name: viewName, attributes: ["foo": "bar"])

// When
for _ in 0..<3 {
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
}
dateProvider.advance(bySeconds: 1)
for _ in 0..<3 {
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
}

// Then
XCTAssertEqual(commandSubscriber.receivedCommands.count, 0)
}
}