Skip to content

Commit

Permalink
iOS: Fixed race condition in setting an image and closing the map
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas committed Oct 21, 2020
1 parent 4a15881 commit 8e579ee
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 43 deletions.
3 changes: 2 additions & 1 deletion ios/RCTMGL/RCTMGLBackgroundLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style backgroundLayer:(MGLBackgroundStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style backgroundLayer:(MGLBackgroundStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{ return [self isAddedToMap];
}];
}

@end
4 changes: 3 additions & 1 deletion ios/RCTMGL/RCTMGLCircleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style circleLayer:(MGLCircleStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style circleLayer:(MGLCircleStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{
return [self isAddedToMap];
}];
}

@end
3 changes: 2 additions & 1 deletion ios/RCTMGL/RCTMGLFillExtrusionLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style fillExtrusionLayer:(MGLFillExtrusionStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style fillExtrusionLayer:(MGLFillExtrusionStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{ return [self isAddedToMap];
}];
}

@end
4 changes: 3 additions & 1 deletion ios/RCTMGL/RCTMGLFillLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style fillLayer:(MGLFillStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style fillLayer:(MGLFillStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{
return [self isAddedToMap];
}];
}

@end
4 changes: 3 additions & 1 deletion ios/RCTMGL/RCTMGLHeatmapLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style heatmapLayer:(MGLHeatmapStyleLayer *)self.styleLayer withReactStyle:self.reactStyle];
[style heatmapLayer:(MGLHeatmapStyleLayer *)self.styleLayer withReactStyle:self.reactStyle isValid:^{
return [self isAddedToMap];
}];
}

@end
2 changes: 2 additions & 0 deletions ios/RCTMGL/RCTMGLLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
- (void)insertLayer;
- (void)setZoomBounds;

- (BOOL)isAddedToMap;

- (nullable MGLSource*)layerWithSourceIDInStyle:(nonnull MGLStyle*) style;

@end
6 changes: 6 additions & 0 deletions ios/RCTMGL/RCTMGLLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ - (void)removeFromMap:(MGLStyle *)style
if (_styleLayer != nil) {
[style removeLayer:_styleLayer];
}
_style = nil;
}

- (BOOL)isAddedToMap
{
return (_style != nil);
}

- (nullable MGLStyleLayer*)makeLayer:(MGLStyle*)style
Expand Down
8 changes: 7 additions & 1 deletion ios/RCTMGL/RCTMGLLight.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ - (void)setMap:(MGLMapView *)map
[self addStyles];
}

- (BOOL)isAddedToMap {
return _map != NULL;
}

- (void)addStyles
{
MGLLight *light = [[MGLLight alloc] init];
RCTMGLStyle *style = [[RCTMGLStyle alloc] init];
[style lightLayer:light withReactStyle:_reactStyle];
[style lightLayer:light withReactStyle:_reactStyle isValid:^{
return [self isAddedToMap];
}];
_map.style.light = light;
}

Expand Down
4 changes: 3 additions & 1 deletion ios/RCTMGL/RCTMGLLineLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style lineLayer:(MGLLineStyleLayer *)self.styleLayer withReactStyle:self.reactStyle];
[style lineLayer:(MGLLineStyleLayer *)self.styleLayer withReactStyle:self.reactStyle isValid:^{
return [self isAddedToMap];
}];
}

@end
3 changes: 3 additions & 0 deletions ios/RCTMGL/RCTMGLMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ - (void) removeFromMap:(id<RCTComponent>)subview
} else if ([subview isKindOfClass:[RCTMGLNativeUserLocation class]]) {
RCTMGLNativeUserLocation *nativeUserLocation = (RCTMGLNativeUserLocation *)subview;
nativeUserLocation.map = nil;
} else if ([subview isKindOfClass:[RCTMGLLight class]]) {
RCTMGLLight *light = (RCTMGLLight*)subview;
light.map = nil;
} else {
NSArray<id<RCTComponent>> *childSubViews = [subview reactSubviews];

Expand Down
3 changes: 2 additions & 1 deletion ios/RCTMGL/RCTMGLRasterLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style rasterLayer:(MGLRasterStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style rasterLayer:(MGLRasterStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{ return [self isAddedToMap];
}];
}

@end
20 changes: 10 additions & 10 deletions ios/RCTMGL/RCTMGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

- (id)initWithMGLStyle:(MGLStyle*)mglStyle;

- (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)circleLayer:(MGLCircleStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)heatmapLayer:(MGLHeatmapStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)rasterLayer:(MGLRasterStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)hillshadeLayer:(MGLHillshadeStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)lightLayer:(MGLLight *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)circleLayer:(MGLCircleStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)heatmapLayer:(MGLHeatmapStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)rasterLayer:(MGLRasterStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)hillshadeLayer:(MGLHillshadeStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
- (void)lightLayer:(MGLLight *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;

- (void)setFillStyleLayerVisibility:(MGLFillStyleLayer *)layer withReactStyleValue:(RCTMGLStyleValue *)styleValue;
- (void)setFillAntialias:(MGLFillStyleLayer *)layer withReactStyleValue:(RCTMGLStyleValue *)styleValue;
Expand Down
50 changes: 30 additions & 20 deletions ios/RCTMGL/RCTMGLStyle.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ - (id)initWithMGLStyle:(MGLStyle*)mglStyle
}


- (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -61,8 +61,10 @@ - (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reac
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self setFillPattern:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self setFillPattern:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand All @@ -75,7 +77,7 @@ - (void)fillLayer:(MGLFillStyleLayer *)layer withReactStyle:(NSDictionary *)reac
}
}

- (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -143,8 +145,10 @@ - (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reac
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self setLinePattern:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self setLinePattern:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand All @@ -159,7 +163,7 @@ - (void)lineLayer:(MGLLineStyleLayer *)layer withReactStyle:(NSDictionary *)reac
}
}

- (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -207,8 +211,10 @@ - (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self setIconImage:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self setIconImage:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand Down Expand Up @@ -329,7 +335,7 @@ - (void)symbolLayer:(MGLSymbolStyleLayer *)layer withReactStyle:(NSDictionary *)
}
}

- (void)circleLayer:(MGLCircleStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)circleLayer:(MGLCircleStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -390,7 +396,7 @@ - (void)circleLayer:(MGLCircleStyleLayer *)layer withReactStyle:(NSDictionary *)
}
}

- (void)heatmapLayer:(MGLHeatmapStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)heatmapLayer:(MGLHeatmapStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -429,7 +435,7 @@ - (void)heatmapLayer:(MGLHeatmapStyleLayer *)layer withReactStyle:(NSDictionary
}
}

- (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -469,8 +475,10 @@ - (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(N
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self setFillExtrusionPattern:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self setFillExtrusionPattern:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand All @@ -491,7 +499,7 @@ - (void)fillExtrusionLayer:(MGLFillExtrusionStyleLayer *)layer withReactStyle:(N
}
}

- (void)rasterLayer:(MGLRasterStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)rasterLayer:(MGLRasterStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -542,7 +550,7 @@ - (void)rasterLayer:(MGLRasterStyleLayer *)layer withReactStyle:(NSDictionary *)
}
}

- (void)hillshadeLayer:(MGLHillshadeStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)hillshadeLayer:(MGLHillshadeStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -585,7 +593,7 @@ - (void)hillshadeLayer:(MGLHillshadeStyleLayer *)layer withReactStyle:(NSDiction
}
}

- (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down Expand Up @@ -615,8 +623,10 @@ - (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDicti
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self setBackgroundPattern:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self setBackgroundPattern:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand All @@ -633,7 +643,7 @@ - (void)backgroundLayer:(MGLBackgroundStyleLayer *)layer withReactStyle:(NSDicti
}
}

- (void)lightLayer:(MGLLight *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)lightLayer:(MGLLight *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand Down
3 changes: 2 additions & 1 deletion ios/RCTMGL/RCTMGLSymbolLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ - (void)addStyles
{
RCTMGLStyle *style = [[RCTMGLStyle alloc] initWithMGLStyle:self.style];
style.bridge = self.bridge;
[style symbolLayer:(MGLSymbolStyleLayer*)self.styleLayer withReactStyle:self.reactStyle];
[style symbolLayer:(MGLSymbolStyleLayer*)self.styleLayer withReactStyle:self.reactStyle isValid:^{ return [self isAddedToMap];
}];
}

- (UIImage *)_createViewSnapshot
Expand Down
2 changes: 1 addition & 1 deletion scripts/templates/RCTMGLStyle.h.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- (id)initWithMGLStyle:(MGLStyle*)mglStyle;

<%_ for (const layer of layers) { _%>
- (void)<%- setLayerMethodName(layer, 'ios') -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyle:(NSDictionary *)reactStyle;
- (void)<%- setLayerMethodName(layer, 'ios') -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid;
<%_ } _%>

<%_ for (const layer of layers) { _%>
Expand Down
8 changes: 5 additions & 3 deletions scripts/templates/RCTMGLStyle.m.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

<% for (const layer of layers) { %>
- (void)<%- setLayerMethodName(layer, 'ios') -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyle:(NSDictionary *)reactStyle
- (void)<%- setLayerMethodName(layer, 'ios') -%>:(<%- getLayerType(layer, 'ios') -%> *)layer withReactStyle:(NSDictionary *)reactStyle isValid:(BOOL (^)(void)) isValid
{
if (![self _hasReactStyle:reactStyle]) {
// TODO throw exception
Expand All @@ -44,8 +44,10 @@
[RCTMGLUtils fetchImage:_bridge url:imageURI scale:[styleValue getImageScale] callback:^(NSError *error, UIImage *image) {
if (image != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_style setImage:image forName:imageURI];
[self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>:layer withReactStyleValue:styleValue];
if (isValid()) {
[self->_style setImage:image forName:imageURI];
[self set<%- iosPropMethodName(layer, pascelCase(layer.properties[i].name)) -%>:layer withReactStyleValue:styleValue];
}
});
}
}];
Expand Down

0 comments on commit 8e579ee

Please sign in to comment.