仅当应用程序在后台运行且时间间隔为一小时时,如何调用方法数据 [英] How to call a method data, only when app is in background, with a time interval of one hour

查看:91
本文介绍了仅当应用程序在后台运行且时间间隔为一小时时,如何调用方法数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新我的应用程序数据,每隔一小时后应用程序在后台运行,但仅当应用程序在后台运行时才刷新.早些时候我是在按钮上轻按的.我在按钮点击时调用了API,但是现在我希望每隔一小时后我的应用程序在后台运行一次,便要设置为API.有可能做到这一点.

I want to refresh my app data, when app is in background after every one hour, but only when app is in background. Earlier I was doing that on a button tap. I was calling an API on the button tap, but now I want the same when my app goes in background after every one hour. Is it possible to do that.

我尝试了下面的代码来调用该方法,但是并没有在后台调用它.

I have tried the below code for calling that method, but it is not getting called in background.

NSTimer* dataTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(autoRefreshAppData) userInfo:nil repeats:YES];

推荐答案

在后台下载内容

是的.

主要用户可以选择使用预定任务或执行带有推送通知的代码块.我认为使用计划任务最简单,但是我已经体验到任务并非总是执行.因此,如果您的应用程序依赖于后台提取,则应检查是否在application:willEnterForeground处下载了数据,如果没有新数据,则应下载数据.

You primary have the options to use a scheduled task or execute a code block with a push notification. In my opinion it's easiest with a scheduled task, but I have have experienced that the task is not always executed. So if your app rely on the background fetch you should check if the data is downloaded at application:willEnterForeground and download data if new data is not available.

以下是指向该主题的Objective-c文档的链接:背景执行

Here's the link to the Objective-c documentation on the topic: Background Execution

Objective-c:

用于创建支持后台下载的配置对象的过程如下:

The process for creating a configuration object that supports background downloads is as follows:

  1. 使用NSURLSessionConfiguration的backgroundSessionConfigurationWithIdentifier:方法创建配置对象.
  2. 将配置对象的sessionSendsLaunchEvents属性的值设置为YES.
  3. 如果您的应用程序在前台运行时开始传输,建议您还将配置对象的自主属性设置为是".
  4. 适当配置配置对象的任何其他属性.
  5. 使用配置对象创建您的NSURLSession对象.
  1. Create the configuration object using the backgroundSessionConfigurationWithIdentifier: method of NSURLSessionConfiguration.
  2. Set the value of the configuration object’s sessionSendsLaunchEvents property to YES.
  3. if your app starts transfers while it is in the foreground, it is recommend that you also set the discretionary property of the configuration object to YES.
  4. Configure any other properties of the configuration object as appropriate.
  5. Use the configuration object to create your NSURLSession object.

配置完成后,您的NSURLSession对象将在适当的时间无缝地将上传和下载任务传递给系统.如果在应用程序仍在运行时(在前台或后台)任务已完成,则会话对象将以通常的方式通知其委托.如果任务尚未完成,并且系统终止了您的应用程序,则系统会自动继续在后台管理任务.如果用户终止您的应用,则系统会取消所有待处理的任务.

Once configured, your NSURLSession object seamlessly hands off upload and download tasks to the system at appropriate times. If tasks finish while your app is still running (either in the foreground or the background), the session object notifies its delegate in the usual way. If tasks have not yet finished and the system terminates your app, the system automatically continues managing the tasks in the background. If the user terminates your app, the system cancels any pending tasks.

与后台会话相关的所有任务都完成后,系统将重新启动已终止的应用程序(假设sessionSendsLaunchEvents属性设置为YES,并且用户未强制退出该应用程序)并调用应用程序委托的方法. (系统可能还会重新启动该应用程序,以处理需要您关注的身份验证挑战或其他与任务相关的事件.)在实现该委托方法时,使用提供的标识符来创建一个新的NSURLSessionConfiguration和NSURLSession对象,其配置与前.系统会将您的新会话对象重新连接到先前的任务,并将其状态报告给会话对象的代表.

When all of the tasks associated with a background session are complete, the system relaunches a terminated app (assuming that the sessionSendsLaunchEvents property was set to YES and that the user did not force quit the app) and calls the app delegate’s application:handleEventsForBackgroundURLSession:completionHandler: method. (The system may also relaunch the app to handle authentication challenges or other task-related events that require your app’s attention.) In your implementation of that delegate method, use the provided identifier to create a new NSURLSessionConfiguration and NSURLSession object with the same configuration as before. The system reconnects your new session object to the previous tasks and reports their status to the session object’s delegate.

自从我使用Swift编写代码以来,我将提供一些文档. Swift 3.0

Since I code using Swift I'll provide some documentation on that. Swift 3.0

  1. 创建计划程序

要初始化调度程序,请为NSBackgroundActivityScheduler调用init(identifier :),并将其以反向DNS表示法传递的唯一标识符字符串(不允许使用零长度和零长度的字符串)传递,该字符串在应用程序启动时保持不变.

To initialize a scheduler, call init(identifier:) for NSBackgroundActivityScheduler, and pass it a unique identifier string in reverse DNS notation (nil and zero-length strings are not allowed) that remains constant across launches of your application.

let activity = NSBackgroundActivityScheduler(identifier:  "com.example.MyApp.updatecheck")

系统使用此唯一标识符来跟踪活动的运行次数,并改进启发式方法,以决定将来何时再次运行该活动.

The system uses this unique identifier to track the number of times the activity has run and to improve the heuristics for deciding when to run it again in the future.

  1. 配置计划程序属性

您可以配置多个属性,请检查API参考. 例如:

There's several properties you could configure, check the API reference for that. E.G:

安排一个每小时触发一次的活动

Scheduling an activity to fire once each hour

activity.repeats = true
activity.interval = 60 * 60

  1. 带有scheduleWithBlock的计划活动:

当您的代码块被调用时,它会作为参数传递给完成处理程序.配置该块以调用此处理程序,并将其传递给NSBackgroundActivityScheduler.Result类型的结果,以指示该活动是完成(完成)还是应推迟(推迟)并在以后重新安排.未能调用完成处理程序导致活动未重新安排.对于要推迟和重新安排的工作,该块可以在调用完成处理程序之前选择调整计划程序的属性,例如间隔或公差.

When your block is called, it’s passed a completion handler as an argument. Configure the block to invoke this handler, passing it a result of type NSBackgroundActivityScheduler.Result to indicate whether the activity finished (finished) or should be deferred (deferred) and rescheduled for a later time. Failure to invoke the completion handler results in the activity not being rescheduled. For work that will be deferred and rescheduled, the block may optionally adjust scheduler properties, such as interval or tolerance, before calling the completion handler.

activity.scheduleWithBlock() { (completion: NSBackgroundActivityCompletionHandler) in
// Perform the activity
self.completion(NSBackgroundActivityResult.Finished)
}

注意事项:

应用仅获得〜10分钟(iOS 7为〜3分钟)的后台执行-此后计时器将停止触发. 从iOS 7开始,当设备锁定时,它将几乎立即挂起前台应用程序.锁定iOS 7应用后,计时器不会触发.

Apps only get ~ 10 mins (~3 mins as of iOS 7) of background execution - after this the timer will stop firing. As of iOS 7 when the device is locked it will suspend the foreground app almost instantly. The timer will not fire after an iOS 7 app is locked.

这篇关于仅当应用程序在后台运行且时间间隔为一小时时,如何调用方法数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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