问题描述
我正在从FireStore查询数据,并使用光标能够对数据进行分页,从而在MapView上显示元素。
这是我的自定义注释类:
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
}
}
我按了一个按钮以加载更多数据,出于测试目的,我的数据池很小,只有7-10个元素。但是,每当我获取/刷新数据时,我总是得到前一组注释的重复。
例如,当我的MapView第一次加载时,我有一批批注,单击刷新按钮,我有一批批注1和批注2,但是也有一批批注1的副本。再次单击刷新,我有一批批注。批注1 x 3,批处理批注2 x 2,而所有数据的一个实例仅属于批注3。
- 我不断收到重复的注释
- 前两个是 已删除
我这样做的原因是为了消除一个事实,即可能/可能在特定视图周围存在许多注释。我仅限于可能存在问题和解决方案的地方。
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
}
我可以检测到元素是否已经在数组中,但是将其删除,即使firstIndex也会使我完全失去注释。
如果您想知道的话,这就是我添加/更新注释的方式:
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)
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)