我意识到我在跟踪器应用程序中滥用从服务器恢复的数据。所以我有这个:
@objc private func updateOnlineTrackWithNewData() {
print("Tick")
if let track = track {
guard track.status == 1 else { return } // is track online?
serverManager.tracks.updateOnlineTrack(for: track, success: { (trackNewPoints) in
//receive new data
guard trackNewPoints.count > 1 else { return }
for point in trackNewPoints {
self.track?.points?.append(point)
self.performSelector(onMainThread: #selector(self.updateOnlineTrackOnMap), with: nil, waitUntilDone: true)
}
}) { (error) in
print(error ?? "Error updating online track")
}
}
}
其中trackNewPoints基本上是一个点数组,我将其附加到主“轨道”,而updateOnlineTrackOnMap只是将其打印出来。 updateOnlineTrackWithNewData()函数由计时器触发,一切正常。但是我想逐点绘制线来模拟实时运动。我怎样才能做到这一点? performSelector似乎不起作用。
如果有帮助的话,我每10秒就有10个新点。
答案 0 :(得分:1)
有一种方法可以做到这一点。创建单独的类抽屉
protocol Drawable { // add to your controller
func appendPoint(_ point: AnyObject)
func updateOnlineTrackOnMap()
}
class Drawer {
private var timer: Timer? = nil
private var points: [AnyObject] = []
func drawPoints(_ points: [AnyObject],
controller: Drawable) {
self.points = points
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
guard let point = self?.points.first else {
timer.invalidate()
return
}
DispatchQueue.global(qos: .userInteractive).async {
controller.appendPoint(point)
controller.updateOnlineTrackOnMap()
self?.points.remove(at: 0)
}
}
}
}