Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Refactor MGLMapboxEvents - #1491

Merged
incanus merged 17 commits into
masterfrom
1ec5-metrics-opt-way-out
May 13, 2015
Merged

incanus merged 17 commits into
masterfrom
1ec5-metrics-opt-way-out

Conversation

@1ec5

@1ec5 1ec5 commented May 9, 2015

Copy link
Copy Markdown
Contributor

This is a significant refactoring of MGLMapboxEvents that ensures it only hangs onto user-specific data when permitted. It consolidates logic around enabling and disabling services and removes some indirection that made it difficult to understand ownership and lifespan of those services. The salient behavioral changes are:

  • MGLMapboxEvents shuts off as soon as the user opts out. (If the user opt-out is implemented in Settings, it shuts off as soon as the user returns to the host app.)
  • After the user turns off Location Services and turns it back on, MGLMapboxEvents restarts its location manager as soon as the user returns to the host app.
  • MGLMapboxEvents defaults to sending events even if the developer has failed to implement a user opt-out.
  • If a Settings.bundle is present but omits the Mapbox Metrics switch, the default defaults to being on.
  • If the developer has indicated that the Mapbox Metrics switch is implemented in-app but in reality it isn’t implemented, the default defaults to being on.

Architectural improvements include:

  • The autonomous MGLMetricsLocationManager singleton no longer exists. MGLMapboxEvents is the clearinghouse for all Metrics-related logic.
  • User-specific data is stored in a self-contained object that is completely discarded whenever MGLMapboxEvents is paused.
  • MGLMapboxMetrics no longer stores the access token separately. Instead, it gets it from MGLAccountManager. MGLMapView forwards access tokens to MGLAccountManager as well.

Some things to test (please chime in if you can think of anything else):

  • Various MGLMapboxAcccountTypes
  • User opt-out while Location Services enabled/disabled
  • User opt-in while Location Services enabled/disabled
  • Disable/enable Location Services while user is opted in/out
    • Disable Location Services, opt out, enable Location Services, opt in
    • Opt out, disable Location Services, opt in, enable Location Services
  • Setting access token via +[MGLAccountManager setAccessToken:] and/or -[MGLMapView setAccessToken:] – ensure MGLAccountManager, MGLMapView, and MGLMapboxEvents are all aware of access token changes

/cc @bleege @jfirebaugh @incanus

@1ec5 1ec5 added iOS Mapbox Maps SDK for iOS refactor telemetry Integration with Mapbox Telemetry libraries labels May 9, 2015
@1ec5 1ec5 self-assigned this May 9, 2015
@1ec5 1ec5 added this to the iOS Beta 2 milestone May 9, 2015
@1ec5

1ec5 commented May 9, 2015

Copy link
Copy Markdown
Contributor Author

I’m placing this PR on beta 2 for now, given its size, but I’m open to pulling it into beta 1 if we get enough testing.

@mb12

mb12 commented May 9, 2015

Copy link
Copy Markdown

When metric collections is paused/disabled, is there any plan to to avoid calls to convert point to lat long, create dictionaries etc? This computation is not really necessary if collection is paused or disabled by the user .

@bleege

bleege commented May 9, 2015

Copy link
Copy Markdown
Contributor

Good stuff @1ec5!

I’m placing this PR on beta 2 for now, given its size, but I’m open to pulling it into beta 1 if we get enough testing.

Totally agree with this decision. Let's not rush things at this stage of the game, especially with something this big. 👍

@1ec5

1ec5 commented May 9, 2015

Copy link
Copy Markdown
Contributor Author

When metric collections is paused/disabled, is there any plan to to avoid calls to convert point to lat long, create dictionaries etc?

With these changes, none of that work should be happening. Most of it is guarded by paused, and additionally we clear out eventQueue and destroy the location manager, so it should never reach the point where event data would need to be constructed.

Incidentally, the rather aggressive approach to jettisoning data on pause means we do need to rebuild all this data on resume. If you’re interested, this is a good opportunity to profile MGLMapboxMetrics for performance issues. Hopefully it keeps a very light touch overall.

@mb12

mb12 commented May 9, 2015

Copy link
Copy Markdown

I am specifically referrring calls to selectors like the two following in MGLMapView.mm. Will calls to these also be protected if collection is paused?

  • (void)trackGestureEvent:(NSString *)gestureID forRecognizer:(UIGestureRecognizer *)recognizer
  • (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

@1ec5

1ec5 commented May 9, 2015

Copy link
Copy Markdown
Contributor Author

In my rudimentary testing/profiling with quite a bit of finger-mashing, -[MGLMapView trackGestureEvent:forRecognizer:] takes up a minuscule amount of time: roughly 20% of the time spent in -handlePanGesture:, for example, and that’s with Mapbox Metrics turned on. -convertPoint:toCoordinateFromView: doesn’t even show up in my time profiles. That said, I think we could find a cleaner way to avoid any additional work when Metrics is off, probably using a delegate pattern. Please file a separate ticket on that. Thanks!

@mb12

mb12 commented May 10, 2015

Copy link
Copy Markdown

Thank you very much @1ec5 for kindly clarifying all my queries. I really appreciate it.

@1ec5 1ec5 mentioned this pull request May 11, 2015
incanus added a commit that referenced this pull request May 12, 2015
@1ec5
1ec5 force-pushed the 1ec5-metrics-opt-way-out branch from 631fa13 to 3a9b03d Compare May 12, 2015 08:34
1ec5 added 9 commits May 12, 2015 01:36
`-[MGLMapboxEvents init]` only gets called if `-[MGLMapboxEvents sharedManager]` sees that user defaults call for an `MGLMapboxEvents` to be created. But those user defaults were only being registered inside `-[MGLMapboxEvents init]`. This change moves the registration to `+initialize`, which is called as soon as the class is loaded.
Removed “get” from names of methods that perform no network or file access.
The shared `MGLMapboxEvents` now owns the only instance of `MGLMetricsLocationManager`. It also owns a `MGLMapboxEventsData` object, to which all user-specific data has been relegated. This object can be built up and torn down on the fly, whenever the opt-out preference is toggled. `MGLMapboxEvents` has also given up knowledge of the access token and now queries `MGLAccountManager` for that information, which `MGLMapView` keeps up-to-date.
`MGLMetricsLocationManager` was nothing but overhead for `CLLocationManager`. Also removed an unused method.
Moved constant values from ivars into constants. Invalidate the flush timer and clear the event queue on pause. Rewrote some flush logic to be easier to follow.
As soon as the user returns to the host app after changing the opt-out preference in Settings, `MGLMapboxEvents` responds by pausing or resuming. Also moved the IB designables agent check to occur only once, at launch, since the process name is not expected to change mid-session.
`MGLMapboxEvents` automatically recreates its location manager when the host app is reauthorized for Location Services. Added some redundancy to ensure that there is no location manager when either Location Services or Mapbox Metrics is off.
@1ec5
1ec5 force-pushed the 1ec5-metrics-opt-way-out branch from 3a9b03d to 54a703b Compare May 12, 2015 08:38
@1ec5

1ec5 commented May 12, 2015

Copy link
Copy Markdown
Contributor Author

I just rebased and force-pushed this branch due to some reverts on master.

@1ec5

1ec5 commented May 12, 2015

Copy link
Copy Markdown
Contributor Author

Note when merging: this assertion needs to stay in -init; otherwise, if it moves along with the rest of the method body to +initialize, developers will have no way of supressing it, no matter how soon they call the setter.

incanus added a commit that referenced this pull request May 12, 2015

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there potential here for this call to be optimized away by ARC since it's not referred to strongly anywhere? Dumb idea, but curious about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, the actual object is held by a static global, so it will never go away for the lifetime of the process.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

Enable Mapbox Metrics by default when Settings.bundle is absent, but allow Settings.bundle to override the default. Don’t register defaults for non-Mapbox preferences. The two-phase registration ensures that the defaults don’t default to unexpected values, yet gives the developer a chance to call `+[MGLAccountManager mapboxMetricsEnabledSettingShownInApp]` before the hard assertion.

Fixes #1518.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we lose some sort of fallback ability here for when the values were nil in the old code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These properties are now set unconditionally in -init based on Info.plist fields that are required for the app to launch. Even if they’re nil for some reason, you’d wind up with a user agent string that starts with “(null)/(null)/(null)”. The old code would simply return nil and crash when inserted into the HTTP request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@incanus

incanus commented May 12, 2015

Copy link
Copy Markdown
Contributor

This feels really solid @1ec5 in code review. Easy to wrap the head around and good improvements made. Doing some testing now.

1ec5 added 3 commits May 12, 2015 16:09
Renamed the property to be consistent with the public class methods. Also removed a spurious #import of a .m file.
@1ec5

1ec5 commented May 12, 2015

Copy link
Copy Markdown
Contributor Author

I folded in #1536, which fixes #1518, because it didn’t make any sense to have the hard assert inside +[MGLMapboxEvents initialize].

incanus added 4 commits May 12, 2015 16:33
 1. Always bail early if in IB.
 2. Always run on main thread if not already.
 3. THEN dispatch_once to create the static instance.
@incanus

incanus commented May 13, 2015

Copy link
Copy Markdown
Contributor

Plan of attack here per chat with @bleege is to merge this to master, cherry pick to ios-b1, and follow up with test adaptation to help in ongoing maintenance. We've had enough 👀 on this today to feel good about it.

incanus added a commit that referenced this pull request May 13, 2015
refactor metrics system to immediately jettison data & tracking when users opt out
@incanus
incanus merged commit a47f749 into master May 13, 2015
@incanus
incanus deleted the 1ec5-metrics-opt-way-out branch May 13, 2015 01:37
incanus added a commit that referenced this pull request May 13, 2015
incanus added a commit that referenced this pull request May 13, 2015
refactor metrics system to immediately jettison data & tracking when users opt out
1ec5 added a commit to 1ec5/react-native-mapbox-gl that referenced this pull request May 22, 2015
MGLMetricsLocationManager.h was removed upstream in mapbox/mapbox-gl-native#1491.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

iOS Mapbox Maps SDK for iOS refactor telemetry Integration with Mapbox Telemetry libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants