为什么上传alamofire后台请求未在后台执行? [英] Why upload alamofire background request don't executes in background?

查看:172
本文介绍了为什么上传alamofire后台请求未在后台执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用alamofire来将30 mb的zip文件上传到服务器

I use alamofire in order to upload 30 mb zip file to server

这是我的请求

let configuration = URLSessionConfiguration.background(withIdentifier: "identifire.com")

    alamoFireManager = Alamofire.SessionManager(configuration: configuration)

let tempZipFilePath = UtilDirectory.tempZipPath.tweak()

    alamoFireManager.upload(URL(fileURLWithPath: tempZipFilePath),
                            to: deligate.url,
                            method: .post,
                            headers: headers)
        .uploadProgress(closure: {
            progress in
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .validate()
        .responseJSON {}

请求开始时,我可以看到上传progress uploadProgress(closure:_),但是如果我关闭应用程序,则上传将暂停。

When request is starting I can see upload progress uploadProgress(closure:_) , but if I close the app uploading is paused.

我希望在关闭应用程序请求后必须完成执行而不会中断。

I expect that after closing the app request has to finish executing without interrupt.

但是为什么后台上传请求停止了?

But why background upload request is stopping?

推荐答案

使用后台任务。您的应用仍然可能被杀死。这取决于操作系统。看看这个 https://developer.apple.com/documentation/uikit / uiapplication / 1623031-beginbackgroundtask

You have to use a background task. Your app still may get killed though. It's up to the OS. Take a look at this https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtask

从UIApplication文档中:

From the UIApplication docs:


使用您的UIApplication对象执行以下操作...扩展应用程序的
执行,以便它可以在后台
(beginBackgroundTask(expirationHandler :),
beginBackgroundTask( withName:expirationHandler:))

Use your UIApplication object to do the following...Extend the execution of the app so that it can finish a task in the background (beginBackgroundTask(expirationHandler:), beginBackgroundTask(withName:expirationHandler:))

这篇关于为什么上传alamofire后台请求未在后台执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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