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

[ios, macos] Raise NSException if layer or source added more than once - #7272

Merged
boundsj merged 3 commits into
release-ios-v3.4.0from
boundsj-no-duplicates
Dec 2, 2016
Merged

boundsj merged 3 commits into
release-ios-v3.4.0from
boundsj-no-duplicates

Conversation

@boundsj

@boundsj boundsj commented Dec 2, 2016

Copy link
Copy Markdown
Contributor

@boundsj boundsj added crash documentation iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling labels Dec 2, 2016
@boundsj boundsj added this to the ios-v3.4.0 milestone Dec 2, 2016
@boundsj boundsj self-assigned this Dec 2, 2016
@mention-bot

Copy link
Copy Markdown

@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."];

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.

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.

Comment thread platform/darwin/src/MGLStyle.h Outdated
/**
Adds a new source to the current style.

@note Adding the same source instance more than once or reusing source

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.

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."];

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.

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.
@boundsj
boundsj force-pushed the boundsj-no-duplicates branch from 1cd00d9 to 3ba0a7c Compare December 2, 2016 21:48
This adds notes to the methods for adding sources and layers to warn
of the hazards of using duplicate instances or identifiers.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

crash documentation iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants