Skip to content

Commit

Permalink
fixed race condition with map#waitForMap (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-ht committed Dec 10, 2021
1 parent cf9904e commit c970a0d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions ios/Classes/MapboxMapController.swift
Expand Up @@ -11,7 +11,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma

private var mapView: MGLMapView
private var isMapReady = false
private var isStyleReady = false
private var isFirstStyleLoad = true
private var onStyleLoadedCalled = false
private var mapReadyResult: FlutterResult?

Expand Down Expand Up @@ -119,9 +119,10 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
case "map#waitForMap":
if isMapReady {
result(nil)
// Style could happen to been ready before the map was ready due to the order of methods invoked
// We should invoke onStyleLoaded
if isStyleReady, !onStyleLoadedCalled {
// only call map#onStyleLoaded here if isMapReady has happend and isFirstStyleLoad is true
if isFirstStyleLoad {
isFirstStyleLoad = false

if let channel = channel {
onStyleLoadedCalled = true
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
Expand Down Expand Up @@ -1088,7 +1089,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
* MGLMapViewDelegate
*/
func mapView(_ mapView: MGLMapView, didFinishLoading _: MGLStyle) {
onStyleLoadedCalled = false
isMapReady = true
updateMyLocationEnabled()

Expand Down Expand Up @@ -1132,16 +1132,14 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma

mapReadyResult?(nil)

// If map is ready and map#waitForMap was called, we invoke onStyleLoaded callback directly
// If not, we will have to call it when map#waitForMap is done
if !onStyleLoadedCalled {
// On first launch we only call map#onStyleLoaded if map#waitForMap has already been called
if !isFirstStyleLoad || mapReadyResult != nil {
isFirstStyleLoad = false

if let channel = channel {
onStyleLoadedCalled = true
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}
}

isStyleReady = true
}

func mapView(_ mapView: MGLMapView, shouldChangeFrom _: MGLMapCamera,
Expand Down

0 comments on commit c970a0d

Please sign in to comment.