Skip to content

Help needed: Threading crashes/deadlocks in SwiftUI UIViewRepresentable on iOS [18/17/16] (Mapbox Maps SDK v11)Β #2407

@Craverush12

Description

@Craverush12

Hi Mapbox team πŸ‘‹,

I'm running into some frustrating threading issues and race conditions when using the Mapbox Maps SDK for iOS (v11.8) wrapped in a SwiftUI UIViewRepresentable, specifically happening on iOS 26

πŸ›‘ The Problem
We have a SwiftUI view (MapboxRouteView) that displays a route using PolylineAnnotationManager and user location using PointAnnotationManager. Our Coordinator is marked with @mainactor to safely bridge state to SwiftUI. However, we are seeing race conditions, deadlocks, or UI unresponsiveness stemming from Mapbox's closures and publishers responding on what seems to be background threads, and then deadlocking when we dispatch back to the main thread.

Specific areas causing trouble:

mapView.mapboxMap.onStyleLoaded.observeNext { ... }
mapView.mapboxMap.onMapLoadingError.observeNext { ... }
mapView.location.onLocationChange.observe { ... }
When these events fire, we sometimes update the SwiftUI state or our Mapbox annotations (annotationManager.annotations = [...]). Wrapping these updates in DispatchQueue.main.async inside the observers isn't fully solving the race conditions, especially as the user's location changes rapidly or the view disappears.

πŸ’» Our Setup
Mapbox SDK Version: v11.8
Integration Profile: SwiftUI UIViewRepresentable

A Snippet of what we are doing:

swift
@mainactor
final class Coordinator {
private var cancellables = Set()
// ...
func bind(to mapView: MapView) {
mapView.mapboxMap.onStyleLoaded.observeNext { [weak self, weak mapView] _ in
DispatchQueue.main.async {
guard let self, let mapView else { return }
self.styleLoaded = true
// Render annotations...
}
}.store(in: &cancellables)

    mapView.location.onLocationChange.observe { [weak self, weak mapView] locations in
        DispatchQueue.main.async {
            guard let self, let mapView, let location = locations.last else { return }
            // Update point annotations...
        }
    }.store(in: &cancellables)
}

}

❓ Questions
Are Mapbox's observe and observeNext callbacks explicitly guaranteed to be called on a specific thread (e.g., main), or do they bounce to background threads?
What is the recommended best practice for safely updating Mapbox AnnotationManagers or moving the camera from within these observation callbacks when in a heavily concurrent environment like SwiftUI?
Are there any known threading/deadlock issues related to onLocationChange or onStyleLoaded specifically on iOS 26?
Any pointers to avoid these race conditions when bridging to SwiftUI would be hugely appreciated! Thank you! πŸ™

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions