在后台会话中下载 AlamoFire [英] AlamoFire Download in Background Session

查看:36
本文介绍了在后台会话中下载 AlamoFire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新应用中使用 Alamofire(基于 Alamofire 的下载管理器示例)我需要一些有关使用后台会话下载文件的说明.我需要覆盖 SessionDelegate 才能让它工作吗?或者只是backgroundCompletionHandler?

I am using Alamofire within a new app (A Download Manager Sample based on Alamofire) I need some clarifications about downloading files using the background session. I need to override SessionDelegate to get it works? Or just backgroundCompletionHandler?

通常使用 Alamofire 在后台处理下载的步骤是什么?以及如何处理我的应用重新发布且下载量不断变化的情况.

Typically what are the steps to handle downloads in background using Alamofire? And how can I handle the case where my app is relauch, with downloads in flux.

推荐答案

更新

基于这个惊人的教程,我在 GitHub.它有一个后台会话管理的例子.

Update

Based on this amazing tutorial, I have put together an example project available on GitHub. It has an example for background session management.

根据 Apple 的 URL 加载系统编程指南:

According to Apple's URL Loading System Programming Guide:

在 iOS 和 OS X 中,当用户重新启动您的应用时,您的应用应立即使用与任何有未完成任务的会话相同的标识符应用程序上次运行,然后为每个应用程序创建一个会话配置对象.这些新会话同样是自动的与正在进行的后台活动重新关联.

In both iOS and OS X, when the user relaunches your app, your app should immediately create background configuration objects with the same identifiers as any sessions that had outstanding tasks when your app was last running, then create a session for each of those configuration objects. These new sessions are similarly automatically reassociated with ongoing background activity.

显然,通过使用适当的后台会话配置实例,您的下载将永远不会不断变化".

So apparently by using the appropriate background session configuration instances, your downloads will never be "in flux".

我还发现这个答案真的很有帮助.

I have also found this answer really helpful.

来自 Alamofire 的 GitHub 页面:

From Alamofire's GitHub page:

应用程序可以创建后台管理器和临时管理器会话,以及自定义默认会话的新管理器配置,例如默认标头 (HTTPAdditionalHeaders) 或超时间隔(timeoutIntervalForRequest).

Applications can create managers for background and ephemeral sessions, as well as new managers that customize the default session configuration, such as for default headers (HTTPAdditionalHeaders) or timeout interval (timeoutIntervalForRequest).

默认情况下,顶级方法使用具有默认会话配置的共享 Manager 实例.但是,您可以创建具有后台会话配置的管理器,如下所示:

By default, top level methods use a shared Manager instance with default session configuration. You can however create a manager with background session configuration like so:

let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.example.app.background")
let manager = Alamofire.Manager(configuration: configuration)

然后您可以使用此 Manager 实例发出请求.

You can then make requests using this Manager instance.

manager.startRequestsImmediately = true
let request = NSURLRequest(URL: NSURL(string: "your.url.here")!)
manager.request(request)

通过查看它的实现,它还有一个属性叫做backgroundCompletionHandler,所以你可以添加一个完成块:

By looking at its implementation, it also has a property called backgroundCompletionHandler, so you can add a completion block:

manager.backgroundCompletionHandler = {
        // do something when the request has finished
    }

这篇关于在后台会话中下载 AlamoFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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