简体   繁体   English

3分钟后继续后台任务

[英]Continue background task after 3 minutes

I tried the answer of this question 我尝试了这个问题的答案

How can a connection started with NSURLConnection while in the foreground be continued in the background? 如何在前台继续在后台继续使用NSURLConnection进行连接?

And works like a charm! 并且像一个魅力! but only for 3 minutes in background, then iOS sends the app to sleep, Im trying to upload n videos and 3 minutes is not enought to send all data, I also readed that I can change my NSURLConnection to NSURLSession but I have no idea how to start that or how much time it takes. 只有3分钟的后台,然后iOS发送应用程序进入睡眠状态,我试图上传n个视频,3分钟不应该发送所有数据,我还说我可以将我的NSURLConnection更改为NSURLSession但我不知道如何开始那个或花多少时间。

Is there a way to keep background task alive after 3 minutes and until upload ends? 有没有办法让后台任务在3分钟后继续存在,直到上传结束?

Is there a way to keep background task alive after 3 minutes and until upload ends? 有没有办法让后台任务在3分钟后继续存在,直到上传结束?

No, there isn't. 不,没有。 On-going background execution is limited to very narrow purposes (eg playing music, VOIP apps, navigation apps, etc.). 正在进行的后台执行仅限于非常狭窄的目的(例如播放音乐,VOIP应用程序,导航应用程序等)。 All of this is outlined in the Background Execution section of the App Programming Guide for iOS. 所有这些都在iOS App程序指南后台执行部分中概述

I also [read] that I can change my NSURLConnection to NSURLSession but I have no idea how to start that or how much time it takes. 我也[读]我可以将我的NSURLConnection改为NSURLSession但我不知道如何开始或花多少时间。

Yes, this is correct. 是的,这是正确的。 If you want network request to continue for more than a few minutes after the app enters background, you should use NSURLSession with a background NSURLSessionConfiguration . 如果您希望应用程序进入后台后网络请求持续超过几分钟,则应使用NSURLSession和后台NSURLSessionConfiguration See Downloading Content in the Background in the App Programming Guide for iOS for more information. 有关详细信息,请参阅“iOS应用程序编程指南” 中的“在后台下载内容 See WWDC 2013 What's New in Foundation Networking for introduction to NSURLSession , including a demonstration on how to do background session. 有关NSURLSession介绍,请参阅WWDC 2013 基础网络的新 NSURLSession ,包括如何进行后台会话的演示。

I'd suggest you tackle this in two steps: First, convert to NSURLSession , and second, enable background NSURLSession operation. 我建议你分两步解决这个问题:首先,转换为NSURLSession ,然后启用后台NSURLSession操作。 I think you'll find this first step is pretty easy. 我想你会发现这第一步非常简单。 All of the concepts are very familiar. 所有的概念都非常熟悉。 The only trick is that NSURLSession has two types of interfaces, delegate-protocol pattern and completion handlers. 唯一的技巧是NSURLSession有两种类型的接口,委托协议模式和完成处理程序。 If you plan on using this for background operation later, you'll want to stick with the delegate-protocol pattern because the simpler completion handler pattern is not compatible with background sessions. 如果您打算稍后将其用于后台操作,则需要坚持使用委托协议模式,因为更简单的完成处理程序模式与后台会话不兼容。 And you'll want to use the upload task method that uploads from a file (don't use the stream-based rendition). 并且您将要使用从文件上载的上载任务方法(不要使用基于流的再现)。

This second step, enabling background operation, isn't hard, but it can be tricky, though, simply because it involves a few new little details that we haven't had to worry about in the past (eg the app delegate code to capture (and later call) the completion handler for background sessions). 第二步,启用后台操作并不难,但它可能很棘手,因为它涉及一些我们过去不必担心的新细节(例如应用程序委托代码捕获(以及稍后调用)后台会话的完成处理程序)。 Also, when you start debugging background session code, it can be disorienting to have requests initiated by one debugging session to show up in the next debugging session (hey, they're background sessions that are supposed to keep running when your app is terminated, after all). 此外,当您开始调试后台会话代码时,在下一个调试会话中显示由一个调试会话启动的请求可能会令人迷惑(嘿,它们是应用程序终止时应该继续运行的后台会话,毕竟)。 It's not a problem, but can be a bit disorienting. 这不是问题,但可能有点迷惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM