2016-11-07 30 views
0

我希望你們能幫助我,因爲我的思維燃燒着大腦:D。 我想通知用戶,例如每100米。但我不想更改位置服務的刷新。有一個簡單的方法,但我不好。我認爲有一個更好的解決方案。 我做的,到目前爲止(這是僞代碼!)這就是所謂的didUpdateLocation斯威夫特地點:每X米火災通知

//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

清理代碼的一種方法是將距離放入一個變量中,並在每次通知時遞增,並減少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 
} 

我想你可以把它稍微更有效的,如果你想要的,但你有什麼是不是世界的盡頭。