本文介绍了即使应用终止,在iOS上发送异步HTTP发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个使用HTTP发布请求以在我们的应用运行时将诊断数据发送到远程服务器的应用上工作.当前,我们将消息排队等待由应用程序中的另一个线程发送(使用我们自己的平台不可知代码进行排队,并使用 curl 发送).

I work on an app that uses HTTP post requests to send diagnostic data to a remote server as our app runs. Currently we queue the message up to be sent by another thread in our app (using our own platform agnostic code to queue and curl to send).

我要解决的问题是,在诊断帖子排队和在其他线程上实际发送消息之间,如果应用程序崩溃(或以其他任何方式终止-尽管崩溃是我主要担心的问题)该信息将永远不会实际发送.应当指出,我们已经在使用crashlytics进行崩溃报告.此诊断信息位于此信息之上(主要是有关应用程序状态,检测到的可恢复错误等的信息.)

The issue I'm trying to solve is that if the app crashes (or is terminated in any other way - though crashes are my main concern) between when the diagnostic post is queued and when it is actually sent on the other thread the information will never actually be sent. It should be noted that we are already using crashlytics for crash reporting. This diagnostic info is on top of that (largely info about the state of the app, detected recoverable errors, etc).

关于如何处理此问题,我有一些想法:

I've had a couple ideas on methods to handle this:

1-在其他平台上,我可能会启动一个新进程来处理发送这些请求,但是我发现您发现的哪些信息似乎不允许您在iOS上进行.

1 - On other platforms I might launch a new process that handles sending these requests, but what information I've found it looks like you are not allowed to do this on iOS.

2-我也可以将这些请求排队在磁盘上,这样如果我们无法立即发送请求,我们可以在下次启动应用程序时将其发送出去.但是,这需要等到用户再次启动我们的应用程序(最重要的是,是否重要)(我们使用此诊断信息的一部分是调试新用户遇到的问题-如果新用户发生崩溃,则很可能他们不会再次打开).

2 - I could also queue up these requests on disk so we can send them the next time our app launches if we fail to send them immediately. However, this requires waiting until when (and importantly if) the user boots our app again (part of what we use this diagnostic info for is debugging issues new users have - if a crash occurs for a new user it's very possible they won't open it again).

iOS是否提供任何使之成为可能的机制?

现在,我们正在使用 curl 发送这些请求,但是如果提供了此请求,我可以切换到iOS特定的API.我找到了 NSURLConnection :: sendAsynchronousRequest ,但看来这仍然发生在我的应用程序中(这意味着我会有同样的问题).

Right now we're using curl to send these requests, but I could switch to an iOS specific API if it provides this. I've found NSURLConnection::sendAsynchronousRequest but it seems like this still all happens within my application (meaning I'd have the same issue).

推荐答案

如果该应用程序未运行,则意味着该应用程序已被用户杀死或从未启动,您无法在后台以开发人员的身份进行任何操作全部.

If the app is "Not Running", meaning that it was killed by the user or was never started, you cannot do anything in the background as a developer at all.

如果您的应用处于已暂停"或背景"状态,则可以请求后台计划时间,然后可以发送.我不是很肯定,但是我认为崩溃不会使您脱离这些状态.

If your app is in the "Suspended" or "Background" states, you can request background scheduling time and may be able to send then. I am not positive, but I think that a crash doesn't remove you from these states.

您的后台功能在AppDelegate中执行.

Your background function is executed in the AppDelegate.

为此:

  1. 在应用程序功能的后台模式"中选中后台提取"框.
  2. 使用 setMinimumBackgroundFetchInterval(_:)设置适合您的应用的时间间隔.
  3. 在应用程序委托中
  4. 实施 application(_:performFetchWithCompletionHandler:)来处理后台提取.
  1. Check the box Background fetch in the Background Modes of your app’s Capabilities.
  2. Use setMinimumBackgroundFetchInterval(_:) to set a time interval appropriate for your app.
  3. Implement application(_:performFetchWithCompletionHandler:) in your app delegate to handle the background fetch.

这将使您尽可能在后台开火.

This will allow you to fire in the background if at all possible.

示例代码,因为这是堆栈溢出:

Sample code since this is Stack Overflow:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

    return true
}

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print ("Backgrounding happened")
    completionHandler(.NewData)
}

请记住,获取间隔是您想要的,而不是您一定要获取的间隔.

Bear in mind that the fetch interval is what you want, not what you will necessarily get.

这篇关于即使应用终止,在iOS上发送异步HTTP发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:04