后台请求无法执行Alamofire Swift

编程入门 行业动态 更新时间:2024-10-27 02:19:26
本文介绍了后台请求无法执行Alamofire Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正尝试在didReceiveRemoteNotification方法中像POST,GET这样的后台进行呼叫,因为随着推送通知的到来,它们开始起作用。我的问题是,直到我打开应用程序,所有Alamofire.request才会在后台模式下调用。我现在有

I'm trying to make calls in background like POST,GET to be more precise in the didReceiveRemoteNotification method, because they start to work as a push notification arrive. My problem is that all the Alamofire.request are never call in Background mode until I open the app. I have by now

我正在尝试打开一个会话,但是它无法使请求正常工作。

Im was trying to open a session but it won't make the request work.

这些就是我要在后台执行的操作(手机在后台执行)

These is what i want to execute in background (cellphone in background)

Alamofire.Manager(configuration: configuration).request(.GET, url, parameters: nil) .responseJSON { (_, _, JSON, _) in //println(JSON) println(JSON) REST OF THE CODE

但是它不起作用,即使我在这些请求下面添加代码也可以,但是

But it won't work, even if i add code below these request it works but the return of the request or even the request is not made.

推荐答案

虽然方法说后台配置,但实际上是什么意思已将网络会话配置为以允许中断和d继续上传/下载。相反,您需要做的是延长应用程序的执行时间,以便即使在后台也可以运行一段时间。

While method says "background configuration", what it actually means is that the network session is configured to allow for interruptions and continuation of upload / download. What you need to do instead is to extend execution time of the application so it works for some time even in background

有 beginBackgroundTaskWithExpirationHandler:

There is beginBackgroundTaskWithExpirationHandler: that is specifically designed to do that. When you use it, you will get few more minutes to execute whatever you need (after that limit, your application will get terminated no matter what, now your application is terminated immediately).

您可以编写以下方法:

func beginBackgroundTask() -> UIBackgroundTaskIdentifier { return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({}) } func endBackgroundTask(taskID: UIBackgroundTaskIdentifier) { UIApplication.sharedApplication().endBackgroundTask(taskID) }

要使用它,您只需简单的开始/结束开始/结束下载调用时的任务:

When you want to use it, you just simple begin / end the task when starting / finishing the download call:

// Start task let task = self.beginBackgroundTask() // Do whatever you need, like download of the images self.someBackgroundTask() ... // End task once everything you need to do is done self.endBackgroundTask(task)

希望

编辑1:

如果问题是从未调用过您的下载方法,则它将表示您没有在通知有效负载中发送正确的数据:

If the problem is that your download method IS NEVER called, then it means you are not sending proper data in notification payload:

触发下载操作,通知的有效负载必须包含值设置为1的内容可用键。当该键存在时,系统会在后台以唤醒应用程序(或将其启动到后台)并调用应用程序委托的应用程序:didReceiveRemoteNotification:fetchCompletionHandler:方法。您对该方法的实现应下载相关内容并将其集成到您的应用中。 developer.apple / library / content / documentation / iPhone / Conceptual / iPhoneOSProgrammingGuide / BackgroundExecution / BackgroundExecution.html

更多推荐

后台请求无法执行Alamofire Swift

本文发布于:2023-11-26 05:46:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   Alamofire   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!