-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make MGLMapView.style property nullable #7664
Conversation
@1ec5, thanks for your PR! By analyzing this pull request, we identified @boundsj, @incanus and @friedbunny to be potential reviewers. |
MGLMapView’s style property is now nullable (optional in Swift). The property is set to nil while the style loads and in the event that the style has failed to load.
ce2ca29
to
1086fe0
Compare
Missed a couple files due to a merge conflict. Rebased and force-pushed the proper fix. |
I think this looks good and makes a lot of sense 👍 I'm not sure about the macOS test failure (especially after efcf574) |
When MGLMapView is created via a nib, -initWithCoder: is called, causing styleURL to be set to nil, in turn causing the default Streets style to be loaded, fooling MGLStyleLayerTests into thinking one-line has been loaded. Instead, create MGLMapView programmatically, passing the intended style URL into the initializer, preventing Streets from being loaded.
#else | ||
NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"MGLStyleLayerTests" owner:self]; | ||
self.mapView.styleURL = styleURL; | ||
NSView *contentView = windowController.window.contentView; | ||
_mapView = [[MGLMapView alloc] initWithFrame:contentView.bounds styleURL:styleURL]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
2b743bc fixes the macOS test failure by creating MGLMapView programmatically for both platforms so that the initializer sets the intended style URL instead of the default Streets URL. |
One downside is that this change, in combination with #6386, would lead to a weird scenario where you could set the |
MGLMapView.style is becoming optional in mapbox/mapbox-gl-native#7664. Avoid having to support optional chaining in Swift by referring to style directly, since that would be the name of the second parameter to -[MGLMapViewDelegate mapView:didFinishLoadingStyle:], which is the most likely place for the developer to put runtime styling code.
I have mixed feelings about making
Is there a way that we can check whether That way, if it's just the RHS of an assignment or a conditional check, it wouldn't trigger the console warning. |
Agreed, it can get repetitive. Fortunately, any code inside guard let style = mapView.style else { return } or: let style = mapView.style!
Yes, it would be possible to warn or assert if any method of MGLStyle is called before it loads. That would be closer to the |
Some integration tests started failing as soon as this PR landed, even though it’s fine on my machine and was fine on the branch. A fix is in #7684. |
MGLMapView’s
style
property is now nullable (optional in Swift) and is set tonil
while the style loads and in the event that the style has failed to load. ThestyleURL
property remains null-resettable (implicitly-unwrapped-optional in Swift) as before.Essentially, the API as it exists has a trap: the developer is tempted to have runtime styling code run synchronously after initializing the map view, for example in
-viewDidLoad
. Unfortunately, any runtime styling API operations they perform while the style loads gets wiped out. Instead, they must wait until the style has finished loading, in-[MGLMapViewDelegate mapView:didFinishLoadingStyle:]
.We want developers to have a way of knowing whether the style has finished loading so they can easily find out why their runtime styling code isn’t working as expected. The problem with adding a warning in
-style
, as #7639 does, is that we’d assume the developer intends to use the MGLStyle object they obtain fromMGLMapView.style
. So if the developer calls thestyle
getter to check whether the style has loaded, they get console spew. It turns out that, if the developer adds a key-value observer (or binding) to thestyle
key, the KVO system calls thestyle
getter even just before the style is set (as a side effect of-willChangeValueForKey:
).This PR avoids that whole mess by preventing the developer from accessing MGLStyle until MGLStyle can be used fruitfully. Swift developers must acknowledge the possibility that
style
isn’t set yet. Unfortunately, Objective-C developers will have little indication as to the problem without stepping in the debugger.Fixes #7512.
/ref #7639 (comment)
/cc @boundsj @ericrwolfe @incanus @frederoni @friedbunny @jfirebaugh