You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Style switching no longer invokes onStyleLoadedCallback on ios #771
If i change the style on an active map the onStyleLoadedCallback does not get called again. (Rebuild a MapboxMap with a different styleString)
I assume that the cause of this is #690@aaverin
I will investigate. But as I see so far a new instance of MapboxMapController is created every time build is called for MapboxMap dart class, so all the style loading should run again. Unless something else gets in the way.
@felix-ht I have just tried on latest master and onStyleLoadedCallback did execute correctly when I changed style, also visually style updated on the screen.
If you have a commit in mind I can quickly switch to it and test. In my app there is a button to switch style that round-rotates over street, outdoor and satelite, so quite easy for me to check.
The styles still do switch fine, but on style loaded no longer gets invoked. As I add my vector data in that callback - it doesn't get added to the new style until i invoke the add vector data function by some other means.
@felix-ht I have logs in my onStyleLoaded callback and when I switch styles I see a new entry logged.
Can you reproduce it on some simplified example? You can use one I have been using for leaks testing #706 (comment)
I will try to reproduce again myself tomorrow
diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift
index 05d0e0c..4c31d6d 100644
--- a/ios/Classes/MapboxMapController.swift+++ b/ios/Classes/MapboxMapController.swift@@ -11,6 +11,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var mapView: MGLMapView
private var isMapReady = false
private var isStyleReady = false
+ private var isStyleReadyNotified = false
private var mapReadyResult: FlutterResult?
private var initialTilt: CGFloat?
@@ -94,7 +95,8 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
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 {+ if isStyleReady && !isStyleReadyNotified {+ isStyleReadyNotified = true
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}
@@ -849,6 +851,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
* MGLMapViewDelegate
*/
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
+ isStyleReadyNotified = false
isMapReady = true
updateMyLocationEnabled()
@@ -883,8 +886,8 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
//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 let mapReadyResult = mapReadyResult {- mapReadyResult(nil)+ if !isStyleReadyNotified {+ isStyleReadyNotified = true
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}
For me onPlatformViewCreated and thus map#waitForMap only get called once. A style switch does not invoke it again. So mapReadyResult never gets set again and on style loaded does not get invoked after a style switch.
On a more general note assuming that the swift code runs in a single thread (on flutters ui thread) - your fix shouldn’t do anything as all of the code is synchronous so its actually impossible for map#waitForMap and 'mapView' to execute concurrently. As it stands right now i would actually be in favor of reverting this commit until be have a better understanding of whats going on with these callbacks.
Interesting, I'll try to investigate tomorrow. Thanks for the example.
The original fix is there because in some cases onStyleLoaded was never invoked on iOS. Debugging on real device, I came up with a fix and since then onStyleLoaded was always invoked.
Looks like there is some other edge-case situation that can happen I didn't take into account.
@felix-ht Debugging, it seems to relate to timing.
Running my app, I have more widgets in build method and a few futures to wait for (permissions, redux store), so there is a more delay and that's why onStyleLoaded calls back correctly.
Running your simplified example there is nothing to wait there for, so it doesn't call correctly.
Debugging your example everything calls back correctly too because of the debug delay.
The reason for the bug seems to be in didUpdateWidget. Changing style doesn't create a new instance of MapboxMapController and rather reuses an old one, updating it's options. Which is why your patch works, re-notifying onStyleLoaded every time style gets ready.
On the other hand I also tried completely removing my extra calls for onStyleLoaded reverting my PR, but then on the real phone I don't see onStyleLoaded called reliably anymore. Again, this could be related to delays in network and/or amount of futures one waits for building the map.
Activity
tonnyavery commentedon Nov 16, 2021
I will investigate. But as I see so far a new instance of MapboxMapController is created every time
buildis called for MapboxMap dart class, so all the style loading should run again. Unless something else gets in the way.tonnyavery commentedon Nov 16, 2021
@felix-ht I have just tried on latest master and
onStyleLoadedCallbackdid execute correctly when I changed style, also visually style updated on the screen.felix-ht commentedon Nov 16, 2021
huh i will try to reproduce - for now i switched back to a branch without the issue
tonnyavery commentedon Nov 16, 2021
If you have a commit in mind I can quickly switch to it and test. In my app there is a button to switch style that round-rotates over street, outdoor and satelite, so quite easy for me to check.
felix-ht commentedon Nov 17, 2021
@aaverin so i did a git bisect and for me Feature/intermediate fixes is the one that breaks the style switching.
The styles still do switch fine, but on style loaded no longer gets invoked. As I add my vector data in that callback - it doesn't get added to the new style until i invoke the add vector data function by some other means.
tonnyavery commentedon Nov 17, 2021
@felix-ht I have logs in my onStyleLoaded callback and when I switch styles I see a new entry logged.
Can you reproduce it on some simplified example? You can use one I have been using for leaks testing #706 (comment)
I will try to reproduce again myself tomorrow
felix-ht commentedon Nov 17, 2021
@aaverin This patch fixes the issue for me:
For me onPlatformViewCreated and thus map#waitForMap only get called once. A style switch does not invoke it again. So mapReadyResult never gets set again and on style loaded does not get invoked after a style switch.
On a more general note assuming that the swift code runs in a single thread (on flutters ui thread) - your fix shouldn’t do anything as all of the code is synchronous so its actually impossible for
map#waitForMapand 'mapView' to execute concurrently. As it stands right now i would actually be in favor of reverting this commit until be have a better understanding of whats going on with these callbacks.Here is also a minimal example to reproduce this: https://github.com/flutter-mapbox-gl/maps/tree/style-switch-example
(i added the style switching to the
Full screen mappage, directly above the critical commit)A snackbar is supposed to be shown whenever onStyleLoaded is called
tonnyavery commentedon Nov 17, 2021
Interesting, I'll try to investigate tomorrow. Thanks for the example.
The original fix is there because in some cases onStyleLoaded was never invoked on iOS. Debugging on real device, I came up with a fix and since then onStyleLoaded was always invoked.
Looks like there is some other edge-case situation that can happen I didn't take into account.
tonnyavery commentedon Nov 18, 2021
@felix-ht Debugging, it seems to relate to timing.
Running my app, I have more widgets in build method and a few futures to wait for (permissions, redux store), so there is a more delay and that's why onStyleLoaded calls back correctly.
Running your simplified example there is nothing to wait there for, so it doesn't call correctly.
Debugging your example everything calls back correctly too because of the debug delay.
tonnyavery commentedon Nov 18, 2021
The reason for the bug seems to be in
didUpdateWidget. Changing style doesn't create a new instance of MapboxMapController and rather reuses an old one, updating it's options. Which is why your patch works, re-notifying onStyleLoaded every time style gets ready.On the other hand I also tried completely removing my extra calls for onStyleLoaded reverting my PR, but then on the real phone I don't see onStyleLoaded called reliably anymore. Again, this could be related to delays in network and/or amount of futures one waits for building the map.
tobrun commentedon Nov 22, 2021
#775 merged, closing