2016-11-07 9 views
0

私の脳は思考から燃えているので、皆さんが私を助けてくれることを願っています。 ユーザーに通知する必要があります。 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 
      } 


     } 

で呼び出されないことは、これよりもはるかに優れたとefficent方法はありますか?または、数学的またはアルゴリズムが役立つかもしれませんか?

これまでのところありがとうございます。

答えて

0

コードをクリーンアップする1つの方法は、変数に距離を入れ、各通知で増分することです。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 
} 

私はあなたがしたい場合、あなたはそれが少しより効率的に作ることができると思うが、何を持っていることは、世界の終わりではありません。

関連する問題