2016-11-07 1 views
0

Ich hoffe, Sie können mir helfen, weil mein Gehirn vom Denken brennt: D. Ich möchte den Benutzer z.B. alle 100 Meter. Aber ich möchte die Aktualisierung des Standortdienstes nicht ändern. Es gibt einen einfachen Weg, aber mir geht es nicht gut. Ich denke, es gibt eine bessere Lösung. Was ich bisher (die Pseudo-Code ist!) Das in didUpdateLocation genannt wirdSwift Standorte: Feuer alle X Meter eine Benachrichtigung

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


     } 

eine viel bessere und effiziente Art und Weise als dies dort ist? Oder gibt es irgendwelche mathematischen oder Algorithmen, die helfen können?

Danke bis jetzt.

Antwort

0

Eine Möglichkeit, den Code zu bereinigen wäre, die Entfernung in eine Variable zu setzen und bei jeder Benachrichtigung zu erhöhen, sollte die if-Anweisungen reduzieren.

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 
} 

Ich glaube, Sie etwas effizienter mehr machen können, wenn Sie wollen, aber was Sie haben, ist nicht das Ende der Welt.

Verwandte Themen