Repository navigation
[ios, macos] Raise NSException if layer or source added more than once - #7272
Conversation
|
@boundsj, thanks for your PR! By analyzing this pull request, we identified @1ec5, @jfirebaugh and @incanus to be potential reviewers. |
| - (void)addToMapView:(MGLMapView *)mapView | ||
| { | ||
| if (_pendingLayer == nullptr) { | ||
| [NSException raise:NSInternalInconsistencyException format:@"Adding the same instance to the style more than once is invalid."]; |
There was a problem hiding this comment.
NSInternalInconsistencyException is the exception name used for assertions. Either define a custom exception name like MGLRedundantLayerException or use NSGenericException. #7258 isn’t critical, so it’s fine to specify MGLRedundantLayerException as a string literal for now without defining a constant.
| /** | ||
| Adds a new source to the current style. | ||
|
|
||
| @note Adding the same source instance more than once or reusing source |
There was a problem hiding this comment.
We could say which exception name gets raised under which circumstance, so the developer knows what to look for. For now, spelling out the exception name would only make sense for the exceptions we’ve given CamelCase names to.
| - (void)addToMapView:(MGLMapView *)mapView | ||
| { | ||
| if (_pendingLayer == nullptr) { | ||
| [NSException raise:NSInternalInconsistencyException format:@"Adding the same instance to the style more than once is invalid."]; |
There was a problem hiding this comment.
If the developer is accidentally readding an instance to a style, they’ll want to know which one. We can describe the layer and style or map view here.
When MGLSource and MGLLayer instances are added to the style, they lose ownership of their std::unique_ptr<T> after it is moved to the mbgl level. Subsequent attempts to add such instances result in a C++ exception. This adds logic in the Darwin platform to raise a NSException if the source and layer addToMapView methods are called more than once and the pointer is invalid. In addition, the documentation in MGLStyle for addSource: and addLayer: has been update to warn developers to avoid adding the same instance twice and, for that matter, instances with the same identifier.
This replaces the integration test of `addSource` when duplicate identifiers are used in `MGLSourceTests` with a more direct unit test in the style class's unit test file `MGLStyleTests`. Although tests of the case where the same instance are actually integration tests where the source instance itself throws, it is easier to test all of this functionality in one place using the public facing MGLStyle API.
1cd00d9 to
3ba0a7c
Compare
This adds notes to the methods for adding sources and layers to warn of the hazards of using duplicate instances or identifiers.
Fixes #7271
cc @1ec5 @frederoni