swift/ios在后台刷新应用程序数据 [英] swift/ios refreshing app data when in background

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

问题描述

我正在编写一个 iOS/Swift 应用程序,它每 X 分钟从 REST 服务读取数据并相应地更新 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.

问题是,如果我理解正确的话,如果没有使用 endBackgroundUpdateTask 停止任务,则最多允许 10/15 分钟后应用程序终止,而我想要无限期地轮询服务的任务(至少在用户从应用程序的设置中禁用它之前)

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 后台获取功能,您可以指定最小后台获取间隔.但是连续调用代码之间的实际间隔将由 iOS 框架确定.有关详细信息,请查看此链接:http://code.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

我在我的应用程序中使用这种方法,我认为这是一种首选方式.

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

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

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