2

I want to check a file on the server only once each day (NSURL). And, no matter if I fetch the file or not, I do not run the function (to check for the file) again that day.

This would SEEM easy... but I forsee issues. Say I had created a directory under the mainBundle: "/Library/runOnceEachDay/". Perhaps I could write to /Library/runOnceEachDay/20100601 knowing that file would only exist if I already ran the function. If it does not exist, run the function and after the function is successful write a zero length file with the date as the filename. Then check before running that function for "mainBundle/Library/runOnceEachDay/YYYYMMDD" whereas YYYYMMDD is today's date. If that file exists, do not run.

I could run a housecleaning routine to clean that directory once/week or something. Any better ideas?

Thanks!

Jann
  • 2,214
  • 3
  • 28
  • 45

2 Answers2

4

The logic is good. You could either do it with files as you say and "Documents" folder is a good location for such files, or you could just have a NSUserDefaults key with the date when you last phoned home.

Jaanus
  • 17,688
  • 15
  • 65
  • 110
  • Isn't Documents BAD as it would be open to viewing by users with the new "drop box" permissions of iTunes -- but thx for responding so quickly. – Jann Jun 02 '10 at 21:17
2

When you start the app, get the previous phone home date/time (NSDate) from NSUserDefaults. (It won't be there the first time). This SO question (option 1) demonstrates how.

If more than 24 hours (or 7 days or whatever) has elapsed since that previous date/time (or you didn't find it there) then phone home and save the current date/time in NSUserDefaults for next startup.

Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147