斯威夫特/ IOS在后台刷新时应用数据 [英] swift/ios refreshing app data when in background

查看:145
本文介绍了斯威夫特/ IOS在后台刷新时应用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写的iOS /斯威夫特应用程序,它从一个REST服务读取数据的每个X分钟,并相应地更新UI。

I'm writing a iOS/Swift application which reads data from a REST service each X minutes and updates the UI accordingly.

现在我想,当应用程序被置于后台,一个任务保持在X分钟的间隔从REST服务读取被调用,并在情况下,数据只是读满足给定的条件,显示出通知提示用户使应用程序回到前台。

Now I would like that when the app is put in the background, a task keeps being invoked at X minutes intervals reading from the REST service and, in case the data just read satisfies a given condition, show a notification prompting the user to bring the app back to the foreground.

在我的搜索我读过 applicationDidEnterBackground 活动期间,我应该开始以 beginBackgroundTaskWithExpirationHandler 的任务。

In my searches I've read that during applicationDidEnterBackground event, I should start a task with beginBackgroundTaskWithExpirationHandler.

问题是,如果我理解正确的话,这允许最多10/15分钟,之后如果任务不与 endBackgroundUpdateTask ,而我要任务,以保持轮询服务无止境(至少直到用户从应用程序的设置来禁用它)

The problem is that, if I've understood correctly, this allows a maximum of 10/15 minutes after which the app is terminated if the task is not stopped with endBackgroundUpdateTask, while I want the task to keep polling the service indefinitely (at least until the user disable it from the app's settings)

这是怎么样的功能正常进行?做一些共同的解决办法或最佳实践这样一​​个问题的解决存在吗?

How is this kind of functionality performed normally? Do some common solutions or best practices exist for the solution of such a problem?

推荐答案

使用iOS的背景提取功能,您可以指定最小的背景提取间隔。但是你的code的连续调用之间的实际间隔将iOS的框架确定。有关详细信息结帐此链接:http://$c$c.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520

Use iOS Background Fetch feature where you can specify minimum background fetch interval. But actual interval between successive invocation of your code will be determined by iOS framework. For details checkout this link: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520

我用这个方法在我的应用程序,我认为这是做的preferred方式。

I use this approach in my app and I think it is a preferred way of doing.

这篇关于斯威夫特/ IOS在后台刷新时应用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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