简体   繁体   English

在iPhone上,如何每天检查一次,是否应该回家?

[英]On iPhone, how do I check once per day to see if i should phone-home?

I want to check a file on the server only once each day (NSURL). 我想每天只检查一次服务器上的文件(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. 这将使SEEM很容易...但是我预见到问题。 Say I had created a directory under the mainBundle: "/Library/runOnceEachDay/". 假设我已经在mainBundle下创建了一个目录:“ / Library / runOnceEachDay /”。 Perhaps I could write to /Library/runOnceEachDay/20100601 knowing that file would only exist if I already ran the function. 也许我可以写到/ Library / runOnceEachDay / 20100601,知道该文件仅在我已经运行该函数的情况下才会存在。 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. 然后在运行该函数之前检查“ mainBundle / Library / runOnceEachDay / YYYYMMDD”,而YYYYMMDD是今天的日期。 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! 谢谢!

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. 您可以按照您所说的文件进行操作,并且“ Documents”文件夹是此类文件的理想存放位置,或者您可以仅拥有一个NSUserDefaults密钥,并带有上次打电话回家的日期。

When you start the app, get the previous phone home date/time (NSDate) from NSUserDefaults. 启动应用程序时,请从NSUserDefaults获取以前的电话归宿日期/时间(NSDate)。 (It won't be there the first time). (它不会第一次出现)。 This SO question (option 1) demonstrates how. 这个SO问题(选项1)演示了如何。

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. 如果自上一个日期/时间以来已经过了24小时(或7天之久)之久(或者您在那儿找不到),请给我打电话,并将当前日期/时间保存在NSUserDefaults中,以便下次启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM