Uploading large video files (over 3 GB) on a server in the background using URLSession in Swift for iOS

huangapple go评论55阅读模式
英文:

Uploading large video files (over 3 GB) on a server in the background using URLSession in Swift for iOS

问题

我目前正在使用Swift开发iOS应用程序,并且需要实现一个功能,允许用户在后台上传大型视频文件(超过3 GB)到服务器。为了实现这一点,我尝试了以下方法:

  • 将视频文件分成相等的块。
  • 将这些块保存在本地。
  • 使用URLSession,我逐个获取并上传每个块。

目前,当我将应用程序置于后台时,上传过程会持续约一分钟,然后停止上传。但是,当我将应用程序重新切换到前台时,它会从停止的地方继续上传。

我面临以下挑战,并希望获得关于潜在解决方案或改进的指导:

  • 如何确保上传过程在后台不间断进行,而不会在一分钟后超时?
  • 在后台上传大型文件时,使用URLSession时需要注意哪些具体考虑因素或限制?
  • 是否有替代方法或库可以简化或优化这个过程,并提供更好的后台上传功能?

非常感谢任何见解、代码片段或相关资源。谢谢!

英文:

I'm currently working on an iOS app using Swift, and I need to implement a feature that allows users to upload large video files (over 3 GB) on a server in the background. To achieve this, I have attempted the following approach:

  • Dividing the video file into equal chunks.
  • Saving these chunks locally.
  • Using URLSession, I fetch and upload each chunk one by one.

Currently, when I put the app in the background, the upload process continues for about a minute, but then it stops uploading. However, when I bring the app back to the foreground, it resumes uploading from where it stopped.

I'm facing the following challenges and would appreciate guidance on potential solutions or improvements:

  • How can I ensure that the upload process continues uninterrupted in the background without timing out after a minute?
  • Are there any specific considerations or limitations I need to be aware of when using URLSession for uploading large files in the background?
  • Are there alternative approaches or libraries that could simplify or optimize this process and provide better background upload capabilities?

Any insights, code snippets, or relevant resources would be greatly appreciated. Thank you!

答案1

得分: 1

背景任务的执行受时间限制,并且再次取决于诸如设备性能和电池[能源]节省等各种因素。因此,在应用程序处于后台时,我们将无法上传文件或保证上传。

要增加后台执行时间,我们可以使用“后台传输服务”:

let configuration = URLSessionConfiguration.background(withIdentifier: "com.example.app.background")
let backgroundSession = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue())
let request = URLRequest(url: URL(string: "https://your.server/upload")!)
let task = backgroundSession.uploadTask(with: request, fromFile: videoURL)
task.resume()

顺便提一下,在应用程序目标设置中启用后台模式。

英文:

The execution of the background task is time bounded and again depends on various factors like device performance and battery[energy] saving etc.. So we would not be able to upload or guaranty to upload the file while app being background.

Now to increase the background execution time we can use the "Background Transfer Service" like

let configuration = URLSessionConfiguration.background(withIdentifier: "com.example.app.background")
let backgroundSession = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue())
let request = URLRequest(url: URL(string: "https://your.server/upload")!)
let task = backgroundSession.uploadTask(with: request, fromFile: videoURL)
task.resume()

BTW enable the background mode in app target settings.

huangapple
  • 本文由 发表于 2023年6月12日 15:16:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定