iOS在后台或应用程序处于活动状态时执行预定的操作 [英] iOS do scheduled operation in background or when app active

查看:53
本文介绍了iOS在后台或应用程序处于活动状态时执行预定的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CoreData模型,我想在12 AM进行更新.因此,即使应用程序可以识别出新的一天即将到来,并且在凌晨12点更改数据模型中的某些内容,这种感觉还是很可能.

最初,这个想法是:准备一个返回更新数据的函数.因此,在函数返回smth I之前,请每次检查时间(两个日期之间的NSDate间隔),然后更新数据模型(如果是新的一天).但是,此架构并不是那么简单,它需要花费一些时间来准备可以获取更新数据的单个点,而且在后台更新CoreData模型也需要花费一些时间,这也增加了此任务的费用.

这个好的解决方案是使用一些计时器来更新12 AM的数据吗?我不关心这种情况下的一致性,但是我不喜欢一个计时器,它每秒钟检查是否已经是12 AM.iOS中是否有一些推送通知更新或某些调度程序管理器可以为我更新数据.再过一次,我只想更新数据层,而不在乎UI的一致性.如果确定一致性很重要,那么我想从最初的想法着手进行数据检索.

为此,我可能需要一些调度程序管理器或重写有关如何获取数据的代码.

解决方案

即使应用程序被用户后台/杀死,也无法定期执行功能.

即使在应用程序后台运行时,定期执行功能的最可靠解决方案是使用为特定时间间隔(在您的情况下,每天午夜)安排的推送通知,这将唤醒应用程序并让其更新它的数据.但是,此解决方案有其缺点,因为您需要一台服务器来发送推送通知,并且用户设备需要连接到Internet.此外,如果用户手动杀死了应用程序,则推送通知不会唤醒该应用程序.

对于您的特定问题,最好的解决方案是以一种具有可用于检索数据的单一函数的方式来重构代码,因此该函数可以确保在一定时间间隔内更新数据.自上次更新以来已通过.

I have CoreData model which I want to update at 12 AM. So it's kind of an even when the app can recognize that a new day is coming and at 12 AM change some things in data models.

Initially, the idea was: Prepare a single function that returns updated data. So before the function returns smth I every time check for the time (NSDate interval between two dates) and then update data model (if it's a new day). But the architecture not so simple for this purpose and it will take some time to prepare for a single point where I can get updated data, also it takes some time in background to update CoreData model which also adds some expenses to this task.

Is this ok solution to use some timer which will update data at 12 AM, I don't care about consistency in this case, but I don't like a timer which is checking every single second is 12 AM already or not. Is there some push notification update or some scheduler manager in iOS which can update data for me. One more time I just want to update the data layer and I don't care about consistency in UI. If consistency matter for sure then I would like to follow initial ide with a single point of retrieving data.

So I probably need some scheduler manager for this purpose or rewrite code of how I get the data.

解决方案

There is no way to execute a function at regular intervals, even when the app is backgrounded/killed by the user.

The most reliable solution for executing a function at regular intervals even when the app is backgrounded is to use push notifications scheduled for the specific time intervals (midnight each day in your case), which would wake up the app and let it update its data. However, this solution has its downsides, since you need a server to send the push notification from and the users device needs to be connected to the internet. Also, push notifications don't wake up the app in case the user manually killed it.

For your particular problem, the best solution would be to refactor your code in a way that you have a single function that can be used to retrieve data and hence this function could ensure the data is updated in case a certain time interval has passed since the last update.

这篇关于iOS在后台或应用程序处于活动状态时执行预定的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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