Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make ios map view array thread safe #180

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions ios/Classes/GoogleMapsNavigationViewRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,45 @@

class GoogleMapsNavigationViewRegistry {
private var views: [Int64: GoogleMapsNavigationView] = [:]
// Using a concurrent queue with a barrier ensures that write operations are serialized,
// meaning each write completes before another write can access the shared resource.
// Multiple read operations can still proceed concurrently as long as no write is in progress.
private let queue = DispatchQueue(
label: "google_navigation_flutter.thread_safe_view_registry_queue",
attributes: .concurrent
)

func registerView(viewId: Int64, view: GoogleMapsNavigationView) {
views[viewId] = view
queue.async(flags: .barrier) { [weak self] in
DispatchQueue.main.async {
self?.views[viewId] = view
}
}
}

func unregisterView(viewId: Int64) {
views.removeValue(forKey: viewId)
queue.async(flags: .barrier) { [weak self] in
DispatchQueue.main.async {
self?.views.removeValue(forKey: viewId)
}
}
}

func getView(viewId: Int64) -> GoogleMapsNavigationView? {
views[viewId]
queue.sync {
views[viewId]
}
}

func getAllRegisteredViewIds() -> [Int64] {
Array(views.keys)
queue.sync {
Array(views.keys)
}
}

func getAllRegisteredViews() -> [GoogleMapsNavigationView] {
Array(views.values)
queue.sync {
Array(views.values)
}
}
}
Loading