I'm querying data from FireStore and displaying the elements on a MapView, using a cursor I am able to paginate data.
This is my custom Annotation class:
final class PostAnnotation: NSObject, MKAnnotation {
var title: String?
var coordinate: CLLocationCoordinate2D
var username: String
var post: PostModel
var subtitle: String?
init(address: PostPlaceMark) {
self.title = address.title
self.coordinate = address.coordinate
self.username = address.username
self.post = address.post
self.subtitle = address.subTitle
}
}
I hit a button to load more data, for testing purposes my data pool is quite small around 7-10 elements. However, whenever I fetch/refresh data, I always get a duplicate of the previous group of annotations.
For instance, when my MapView first loads I have batch of annotations, hit my refresh button I have batch of annotations 1 and batch of annotations 2 but also have a duplicate of batch of annotations 1. Hit refresh again I have batch of annotations one x 3, batch of annotations two x 2 and just one instance of all data belonging to batch of annotations three.
I have managed to figure out where the issue lies but I have two extremes here:
- One I keep getting duplicate annotations
- Two the previous batch is removed
The reason I am doing this is to combat the fact there may/can be a number of annotations surrounding a particular view. I have limited to where the issue and potential solution may lie.
private static func annotationsByDistributingAnnotationsContestingACoordinate(annotations: [PostAnnotation], constructNewAnnotationWithClosure ctor: annotationRelocator) -> [PostAnnotation] {
var newAnnotations = [PostAnnotation]()
let contestedCoordinates = annotations.map{ $0.coordinate }
let newCoordinates = coordinatesByDistributingCoordinates(coordinates: contestedCoordinates)
newAnnotations.removeAll()
for (i, annotation) in annotations.enumerated() {
let newCoordinate = newCoordinates[i]
let newPost = annotation.post
let newAnnotation = ctor(annotation, newCoordinate, newPost)
print(newAnnotation.post.location)
newAnnotations.forEach { (naac) in
annotations.forEach { (element) in
if newAnnotations.contains(where: {$0.post.postID == naac.post.postID}) {
let index = newAnnotations.firstIndex{$0.post.postID == naac.post.postID}
// newAnnotations.remove(at: index!)
print("removed")
// newAnnotations.removeAll(where: {$0.post.postID == naac.post.postID})
}
}
}
newAnnotations.append(newAnnotation)
}
let withoutDuplicates = Array(Set(newAnnotations))
return withoutDuplicates
}
I can detect if the element is already in the array, however, removing it, even the firstIndex causes me to lose the annotation completely.
This is how I add/update my annotations, in case you was wondering:
private func updateAnnotations(from mapView: MKMapView) {
let annotations = self.address.map(PostAnnotation.init)
let newAnnotations = ContestedAnnotationTool.annotationsByDistributingAnnotations(annotations: annotations) {
(oldAnn: MKAnnotation, newCoordinate:CLLocationCoordinate2D, post: PostModel) -> (PostAnnotation) in
PostAnnotation(address: PostPlaceMark.init(post: post, coordinate: newCoordinate, title: "\(post.manufacturer) \(post.model)", username: post.username, subTitle: "£\(post.price)"))
}
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(newAnnotations)
}