-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make source ownership consistent and make MGLGeoJSONSource's content properties mutable #6793
Changes from 14 commits
1b34cf7
37c8b9f
517524c
d460b78
4ec9d6a
1dc68b5
1f447a4
3376545
ab8075a
65f7915
d384f44
4c27bb8
3c7a900
530f0a9
19dc2cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,7 @@ - (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier | |
- (MGLSource *)sourceWithIdentifier:(NSString *)identifier | ||
{ | ||
auto mbglSource = self.mapView.mbglMap->getSource(identifier.UTF8String); | ||
|
||
if (!mbglSource) { | ||
return nil; | ||
} | ||
|
@@ -159,8 +160,8 @@ - (MGLSource *)sourceWithIdentifier:(NSString *)identifier | |
NSAssert(NO, @"Unrecognized source type"); | ||
return nil; | ||
} | ||
|
||
source.source = mbglSource; | ||
source.rawSource = mbglSource; | ||
|
||
return source; | ||
} | ||
|
@@ -205,12 +206,21 @@ - (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLaye | |
|
||
- (void)addSource:(MGLSource *)source | ||
{ | ||
self.mapView.mbglMap->addSource(source.mbglSource); | ||
// After it is added, the pending source is moved to the mbgl map and is | ||
// no longer "pending". The move operation sets it to NULL. However, the | ||
// MGLSource's rawSource will still point to the mbgl source and clients | ||
// can mutate it via that reference. | ||
self.mapView.mbglMap->addSource(source.pendingSource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @jfirebaugh. a43911e clarifies the transfer of ownership at the call site but might introduce other readability issues. I don't think we can do this with the @Property since it represents a single set/return signature for the value. I just did it this way (adding setter/getter) to make it work and to get a sense of how it would look. Maybe there is a better, alternative approach that allows the If not, I'm tempted to revert a43911e unless you feel strongly about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here’s an idea that still requires indirection but limits the gymnastics to well-worn techniques:
Do you anticipate any other reasons we’d need to get the pending source like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with deleting a43911e @1ec5. IMO, the simplicity of working with the Relatedly:
I don't think there will be other reasons to get the pending source. The platform code should always deal with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
To me, that suggests we may want to go with the approach I outlined above. Not only does it make it clear where ownership is transferred, but it also prevents misuse of the pendingSource pointer. Currently you can use that property however you like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true. I'll go with your approach then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 19dc2cb |
||
} | ||
|
||
- (void)removeSource:(MGLSource *)source | ||
{ | ||
self.mapView.mbglMap->removeSource(source.identifier.UTF8String); | ||
|
||
// Once a mbgl source is removed from the map, ownership does not return | ||
// to the MGL source. Therefore, the rawSource pointer is set to NULL | ||
// so that the implementation of MGL source can avoid using it again. | ||
source.rawSource = NULL; | ||
} | ||
|
||
- (NS_ARRAY_OF(NSString *) *)styleClasses | ||
|
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.
-[MGLSource initWithIdentifier:]
should implement a private-commonInit
stub so that-[MGLSource initWithIdentifier:]
should call it and consolidate all these disparate calls. Each MGLSource subclass can still override the method to do things specific to the particular kind ofmbgl::style::Source
it’s working with.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.
That would be a nice refactor. However,
commonInit
relies on having properties likeURL
andgeoJSONData
andfeatures
set before it runs. IfMGLSource
'sinitWithIdentifier:
callscommonInit
before the subclass init can set the requisite properties, then unexpected results will occur. I can imagine some ways to work around this but I think the disparate calls may actually be the best way to go, for now.