swift - How do I reset a variable at the start of each day? -


i have variable keeps track of user statistic want reset @ beginning of each day. how can that? since application not allowed run in background, seems have check every time application active don't know how reset variable have once. function wanted use:

    let beginingofday = nscalendar.currentcalendar().startofdayfordate(nsdate())     func resetcurrenttime(){      // date comparision compare current date , begining of day.     let datecomparisionresult:nscomparisonresult = nsdate().compare(beginingofday)      if datecomparisionresult == nscomparisonresult.ordereddescending || datecomparisionresult == nscomparisonresult.orderedsame {         // current date greater or equal end date.         currenttime = 0 //reset time tracker     } } 

i wanted use function check when application launched problem application launched many time day. how can reset variable once @ beginning of day if user using application or when application becomes active or launched first time day? thanks

you can store in user defaults value.

so flow following:

  1. when app launched or became active check whether value of variable in user defaults same current day (e.g. 25/07/2016), nothing.
  2. if value different, update value in user defaults current day.

if app running , date changed, can update value of variable subscribing notification: uiapplicationsignificanttimechangenotification


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -