我希望你们能帮助我,因为我的大脑正在思考:D。 我想通知用户,例如每100米。但我不想更改位置服务的刷新。有一个简单的方法,但我不喜欢它。我认为有更好的解决方案。 到目前为止我做了什么(那个伪代码!)这是在didUpdateLocation中调用的
//We want notify User but only 5 times during the movement
if distanceToReport < settedDistance {
//Store that first Notification is fired
if !firstNotificationFired {
self.notifyUser(report: report)
}
if firstNotificationFired && distanceToReport <= 400 {
//Store that second Notification is fired
self.notifyUser(report: report)
} else if secondNotificationFired && distanceToReport <= 300 {
//Store that third Notification is fired
} else if thirdNotificationFired && distanceToReport <= 200 {
//Store that fourth Notification is fired
} else if fourthNotifiationFired && distanceToReport <= 100 {
//Store that fifth Notification is fired
}
}
有比这更好,更有效的方法吗?或者是否有任何可能有帮助的数学或算法?
到目前为止,谢谢。
答案 0 :(得分:0)
清理代码的一种方法是将距离放入变量并在每个通知处递增,应该减少if语句。
if notificationShouldFire && distanceToReport <= targetDistance {
self.notifyUser(report: report)
targetDistance += targetDistance + 100
notificationShouldFire = true
// could also use count variable rather than a variable for each notif.
firedNotifications += firedNotifications + 1
}
// your if statement would change slightly
if firedNotifications <= 5 && distanceToReport <= targetDistance {
// increment and notify here
}
我认为如果你愿意,你可以提高效率,但你所拥有的并不是世界末日。