gpt4 book ai didi

ios - swift 位置 : Fire every X Meters a Notification

转载 作者:行者123 更新时间:2023-11-30 12:55:22 26 4
gpt4 key购买 nike

我希望你们能帮助我,因为我的大脑因思考而燃烧: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
}


}

还有比这更好、更有效的方法吗?或者有什么数学或算法可能有帮助吗?

到目前为止,谢谢。

最佳答案

清理代码的一种方法是将距离放入变量中,并在每次通知时递增,应该减少 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
}

我认为,如果你愿意,你可以稍微提高效率,但你所拥有的并不是世界末日。

关于ios - swift 位置 : Fire every X Meters a Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com