This repository was archived by the owner on Aug 8, 2023. It is now read-only.
visibleAnnotations unexpectedly return nil #14531
Copy link
Copy link
Closed
Description
After adding an annotation to a visible region of an empty map, visibleAnnotations still return nil, meaning that no annotation is visible.
But the more confusing thing is that the counting will be correct if sleep for a moment after adding the annotation.
Steps to reproduce
- Count immediately after adding annotation - return nil
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 23, longitude: 160)
setCenter(annotation.coordinate, animated: false)
addAnnotation(annotation)
print(visibleAnnotations?.count ?? "nil") // return nil- Add annotation, take a rest, then count - return 1 as expect
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 23, longitude: 160)
setCenter(annotation.coordinate, animated: false)
addAnnotation(annotation)
DispatchQueue.global().async {
sleep(1)
DispatchQueue.main.async {
print(self.visibleAnnotations?.count ?? "nil") // return 1
}
}Expected behavior
visibleAnnotations should return the accurate counting of visible annotations, which should be 1 in the code above.
Configuration
Mapbox SDK versions: 4.10
iOS/macOS versions: iOS 12.2
Xcode version: 10.2.1
Activity
1ec5 commentedon May 13, 2019
Most likely, under the hood, mbgl is adding the annotation source and layer asynchronously. The same thing would happen if you add a style layer and immediately try to query for its visible features. The annotation API is designed to be synchronous because that’s how it was originally implemented, but since migrating it to the runtime styling API, it’s possible that the synchronicity is misleading.
julianrex commentedon May 13, 2019
@pozdnyakov, will your refactoring help here? i.e. will we be able to query for features that have been added, but have yet to be rendered? This is a common use case (and misunderstanding). Certainly, it feels like I should be able to query for something I have just added.
julianrex commentedon Jul 1, 2019
Behind the scenes
visibleAnnotationscallsqueryRenderedFeatures, which returns cached results. You have to wait about 300ms before results are valid.See the following related issues:
#10778 (comment)
#12103
Closing since this is expected and current behavior - though we would like to change this, as it's clearly confusing and error-prone.