In my application I have a map that shows your points with some different images. When I make some changes in some parameter that is used to define the image I try to update the map by removing and adding the notations. The problem is that when I update the annotations and add them again the method viewFor annotation receives the annotation as nil. What can I be doing wrong?
Here I am updating my map after making the changes:
func reloadMap(){
        for annotation in mapView.annotations {
            let a = annotation
            self.mapView.removeAnnotation(a)
            DispatchQueue.main.async {
                self.mapView.addAnnotation(a)
            }
        }
    }
This is my viewForAnnotation that I use to set the annotation image:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationIdentifier = "pin"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.canShowCallout = true
            var categoryName = ""
            var colorName = ""
            // Resize image
            if annotation is MyAnnotation{
                let index = (annotation as! MyAnnotation).annotationIndex
                let annotationData = searchResult[index]
                let categories = annotationData.object(forKey: "category") as! NSArray
                for item in categories {
                    let category = item as! String
                    if hasCategory(category: category) {
                        categoryName = translateCategoryName(category: category)
                        break
                    }
                }
                let status = annotationData.object(forKey: "status") as! String
                if status == "Lotado"{
                    colorName = "vermelho"
                } else if status == "Quase cheio"{
                    colorName = "laranja"
                } else if status == "Médio"{
                    colorName = "amarelo"
                }else if status == "Vazio"{
                    colorName = "verde"
                } else {
                    colorName = "cinza"
                }
            }
            var imageName = ""
            if categoryName != "" {
                imageName = "\(colorName)-\(categoryName)"
            } else {
                imageName = "verde-padaria"
            }
            if annotation is MKUserLocation {
                let userAvatar = defaults.integer(forKey: "avatarNumber")
                imageName = "balao-avatar\(userAvatar)"
            }
            let pinImage = UIImage(named: imageName)
            let size = CGSize(width: 30, height: 50)
            UIGraphicsBeginImageContext(size)
            pinImage?.draw(in: CGRect.init(x: 0.0, y: 0.0, width: size.width, height: size.height))
            let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            annotationView?.image = resizedImage
            if !(annotation is MKUserLocation) {
                let btn = UIButton(type: .infoLight)
                btn.addTarget(self,action:#selector(showEstablishment),for:.touchUpInside)
                annotationView?.rightCalloutAccessoryView = btn
            }
            return annotationView
        }
        else {
            annotationView?.annotation = annotation
        }
        return annotationView
    }