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

Source and layer ownership is inconsistent (from style versus from developer) #6254

Closed

Description

@1ec5

MGLSource and the MGLStyleLayer classes manage their underlying C++ objects inconsistently depending on the task. For example, when getting an MGLSource object from an MGLStyle, the MGLSource object holds a strong reference to an mbgl::style::Source object in its source property. On the other hand, when the developer initializes an MGLSource object from scratch in order to add it to an MGLMapView, -[MGLSource mbglSource] creates the mbgl::style::Source object lazily when adding it to the style and never holds a reference to it. If the developer attempts to get an MGLSource from the style and add it back to the style, a brand-new mbgl::style::Source object is created and added to the map, discarding any modifications that the developer may have made on the MGLSource object.

We should remove -[MGLSource mbglSource]. MGLSource should create the mbgl::style::Source upfront in its initializers. (There should be an additional private initializer that accepts an mbgl::style::Source.) This means all the source options need to be immutable. Currently, the only mutable source option is the sourceIdentifier property, but I think that should be read-only anyways.

The catch is that a source added to the map really shouldn’t be owned by the MGLSource object. Could we treat the source property as holding only a “pending” source and always access the mbgl::style::Source object via mbgl::Map::getSource()? What happens if the developer attempts to add the same MGLSource to the MGLStyles of two different MGLMapViews?

/cc @frederoni @boundsj

Activity

  1. added
    iOS
    macOS
    on Sep 6, 2016
  2. added this to the ios-v3.4.0 milestone on Sep 6, 2016
  3. jfirebaugh commented on Sep 7, 2016

    @jfirebaugh
    Contributor

    To expand on #6269 (comment), objects wrapping mbgl::style::Layer and mbgl::style::Source domain objects need to have two internal members:

    • A std::unique_ptr<>, which is present only for objects that were created independently (not obtained by Map#getLayer or Map#getSource) and have not yet been added via Map#addLayer or Map#addSource.
    • A raw pointer to the mbgl object, which is always initialized, either to the result of Map#getLayer or Map#getSource, or for independently created objects, to the pointer value held in the unique_ptr. In the latter case, this raw pointer value stays even after ownership of the object is transferred via Map#addLayer or Map#addSource.

    This is the approach that the Android bindings follow.

  4. jfirebaugh commented on Sep 7, 2016

    @jfirebaugh
    Contributor

    Note that after a source or layer is removed from the style, you don't get ownership of that object back, and any raw pointers you retained are invalid. This has implications for documentation of the SDK bindings for the removal methods.

  5. 1ec5 commented on Sep 7, 2016

    @1ec5
    ContributorAuthor

    By the way, a somewhat orthogonal idea: for MGLStyleLayers obtained from the MGLStyle, the only state MGLStyleLayer maintains would be the layer identifier, and it always uses that identifier to obtain the mbgl::style::Layer and manipulate it locally. This sort of proxy pattern is common in frameworks like Scripting Bridge.

  6. 1ec5 commented on Sep 26, 2016

    @1ec5
    ContributorAuthor

    Textbook example of what developers face with the current inconsistency: #6460.

  7. self-assigned this
    on Oct 21, 2016
  8. added
    release blocker
    on Oct 24, 2016
  9. removed their assignment
    on Oct 30, 2016
  10. boundsj commented on Nov 7, 2016

    @boundsj
    Contributor

    The layer portion of this was fixed by #6904 and the source portion in #6793.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    iOSmacOSrefactorrelease blockerruntime styling

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions