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-1765 Fix foreground app state reporting in AppStateListener - part 5 🏁 #696

Conversation

ncreated
Copy link
Member

@ncreated ncreated commented Dec 20, 2021

What and why?

📦 This is the final PR closing topic of tracking app launch events (and crashes). While testing it (with new options added to debug UI 👇), I found a bug 🐞 in AppStateListener, which I'm solving in this PR.

Screenshot 2021-12-20 at 16 41 10

How?

Problem was that AppStateListener was only considering UIApplication.State.active as "foreground" application state, whereas app lifecycle is more complex than that and it can be also in "foreground" while in .inactive state:

applicationState
The app is inactive at launch, and then is generally in either an active or background state. The app may become inactive for a short period — for example, when transitioning between active and background states, when the system presents an alert in front of it, or when the system displays the application switcher.

Because .inactive was not considered "foreground", any event tracked during application launch (e.g. from application(_:didFinishLaunching:)) was starting "Background" instead of "ApplicationLaunch" view. I fixed this issue by exposing AppState directly from AppStateListener and letting other components decide by their own on handling particular states.

🏁 Final Testing

With all logic in place, I made last tests, ensuring everything works as expected. Here are some explored scenarios:

  • Tracking app launch event in foreground:

foreground-launch-event

  • Tracking app launch crash in foreground:

foreground-launch-crash

  • Tracking app launch event in background:

background-launch-event

  • Tracking app launch crash in background:

background-launch-crash

  • Tracking background event:

background-event

  • Tracking background crash:

background-crash

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference

@ncreated ncreated self-assigned this Dec 20, 2021
@ncreated ncreated marked this pull request as ready for review December 20, 2021 15:49
@ncreated ncreated requested a review from a team as a code owner December 20, 2021 15:49
…ion on app foreground state

as `.active` is not enough to infer if app is in foreground. It might be also in foreground while
the app state is `.inactive`.
@ncreated ncreated force-pushed the ncreated/RUMM-1765-fix-foreground-app-state-reporting-in-app-state-listener branch from 1dacc68 to 7a6548a Compare December 21, 2021 16:25
Copy link
Member

@maxep maxep left a comment

Choose a reason for hiding this comment

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

Good catch 👌


XCTAssertEqual(history2.currentState.date.timeIntervalSince(history1.currentState.date), 1.0)
// When
(0..<1).forEach { _ in
Copy link
Member

Choose a reason for hiding this comment

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

This will do a single iteration :)

Suggested change
(0..<1).forEach { _ in
(0..<10).forEach { _ in

Copy link
Member

Choose a reason for hiding this comment

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

This one need to be addressed before merging. All good otherwise!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ops! Fixed, good catch 💪

)
func testWhenAppTransitionsBetweenForegroundAndBackground_itComputesTotalForegroundDuration() {
let numberOfForegroundSnapshots: Int = .mockRandom(min: 0, max: 20)
let numberOfBackgroundSnapshots: Int = .mockRandom(min: numberOfForegroundSnapshots == 0 ? 1 : 0, max: 20) // must include at least one snapshot
Copy link
Member

Choose a reason for hiding this comment

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

Why not just

Suggested change
let numberOfBackgroundSnapshots: Int = .mockRandom(min: numberOfForegroundSnapshots == 0 ? 1 : 0, max: 20) // must include at least one snapshot
let numberOfBackgroundSnapshots: Int = .mockRandom(min: 1, max: 20) // must include at least one snapshot

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to also cover scenario where numberOfBackgroundSnapshots is 0. And this couldn't be done if numberOfForegroundSnapshots is 0 too, because we need at least one snapshot for:

initialSnapshot: allSnapshots[0],

In general, in this fuzzy test I wanted to cover all possibilities:

  • app only in foreground,
  • app only in background,
  • app in both foreground and background.

Base automatically changed from ncreated/RUMM-1765-send-app-launch-crashes to ncreated/RUMM-1765-app-launch-events-tracking December 22, 2021 11:40
@ncreated ncreated merged commit 0b4ae5e into ncreated/RUMM-1765-app-launch-events-tracking Dec 22, 2021
@ncreated ncreated deleted the ncreated/RUMM-1765-fix-foreground-app-state-reporting-in-app-state-listener branch December 22, 2021 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants