每天在Swift中执行特定方法 [英] Executing a specific method daily in Swift

查看:104
本文介绍了每天在Swift中执行特定方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您将如何拥有每天执行一次的功能.在我的情况下,我希望用户回答日常问题,而答案将留给用户一天.第二天,将提出一个新问题,依此类推.有人告诉我,当用户不使用该应用程序时,您将无法执行功能.所以我想知道是否有一种方法可以设置一个过期的计时器(例如24小时),并从根本上删除答案?如果有什么办法可以解决这个问题,我非常感谢您向大家学习.

I was wondering how you would go about having a function that executes once a day. In my case I would like users to answer a daily question and the answer will stay with the user for a day. The next day, a new question will be asked and so on. I've been told that you cannot execute functions when the user is not using the app. So I was wondering if there is a way to set a timer (e.g. a 24-hour) that expires and essentially deletes the answer? If there is any way you would go about this issue, I would really appreciate learning form you guys.

当前,我正在使用Parse.com来检索我的问题,我不确定是否应将答案保存回分析中或将其作为NSUserDefaults进行处理,因为我不知道如何解决上述问题.

Currently I am using Parse.com to retrieve my questions and I am unsure whether or not I should save the answers back in parse or have them as NSUserDefaults because I don't know how to go about the issue stated above.

推荐答案

有两种方法:

  • 本地通知:您可以安排本地通知(如果用户向应用授予了通知特权)将在某个预定时间向用户显示通知.如果用户点击通知,则将打开该应用程序. (不过,如果用户未点击通知,则该应用将不会打开.)

  • Local notifications: You can schedule a local notification that (if the user grants notification privileges to the app) will present a notification to the user at some predetermined time. If the user taps on the notification, the app will be opened. (If the user doesn't tap on the notification, though, the app will not be opened.)

请参见本地和远程通知编程指南.

背景获取:您可以拥有应用程序请求后台获取功能,在该功能中,操作系统将允许应用程序非常快速地进行网络请求,以查看是否有任何数据要检索.您可以将其与本地通知结合使用,以便在有新问题出现时让用户知道.请注意,您无法控制此操作的时间安排,因此不能保证每天都进行,更不用说每天都在一致的时间进行了.尽管如此,这是应用程序检查新数据的一种简便方法,而无需用户自己运行该应用程序.

Background fetch: You can have the app request background fetch capabilities, in which the OS will allow the app to make very quick network requests to see if there is any data to retrieve. You can combine this with local notifications to let the user know when there is new question available. Note, you cannot control the timing of this, so it's not guaranteed to take place daily, much less at a consistent time every day. Nonetheless, this is an easy way for the app to check for new data without the user having to run the app themselves.

请参见与本地通知有关,您还可以使用推送通知来娱乐(当有新问题出现时,您的Web服务将主动通知用户).在前面的

Related to the local notifications, you could also entertain using push notifications (where your web service would proactively notify the user when there was new question available). This push notification service is discussed in the aforementioned Local and Remote Notification Programming Guide.

当用户启动该应用程序时,您可以将当前日期和时间与与最后一个已知问题相关联的日期和时间进行比较,以查看您是否提出了最后一个问题或是否发起了网络请求以获取最新信息.下一个.

When the user starts up the app, you can compare the current date and time to the date and time associated with the last known question to see whether you present the last question or whether you initiate network request to get the next one.

关于答案的存储位置,NSUserDefaults几乎肯定是错误的地方.如果要在本地保存,请将其保存到永久性存储(档案等)或使用Core Data.但是,将其保存回网络的好处是可以跨设备同步应用程序的问题和答案的状态.

Regarding where to store the answers, NSUserDefaults is almost certainly the wrong place. If you want to save it locally, save it to persistent storage (an archive or the like) or use Core Data. Saving it back to the network has the virtue, though, of the possibility to synchronize the state of the app's questions and answers across devices.

这篇关于每天在Swift中执行特定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆