[英]Is it possible to use AVAssetExportSession's `exportAsynchronouslyWithCompletionHandler:` method when the app is in the background?
I'm working on an iOS app, uploading videos from the Camera Roll, using NSURLSession with a background configuration. 我正在使用iOS应用,使用NSURLSession和后台配置从Camera Roll上传视频。 The user can queue up multiple videos for upload (the queue is executed serially). 用户可以将多个视频排队等待上传(该队列是连续执行的)。
A single upload consists of: 单个上载包括:
requestAVAssetForVideo
method. 使用PHImageManager的requestAVAssetForVideo
方法获取对PHAsset的AVURLAsset引用。 I can queue up multiple videos and the process works well in the foreground. 我可以将多个视频排队,并且该过程在前台运行良好。 They complete one after another. 他们一个接一个地完成。
But if I queue up several videos and then background the app. 但是,如果我排队几个视频,然后使应用程序后台运行。 As soon as execution reaches the exportAsynchronouslyWithCompletionHandler:
stage it stalls until I foreground the app again. 一旦执行达到exportAsynchronouslyWithCompletionHandler:
阶段,它就会停顿,直到我再次看到应用程序为止。 (I know this because I'm posting debug statements in local notifications, visible on the lock screen). (我知道这是因为我要在本地通知中发布调试语句,在锁定屏幕上可见)。
Is it possible to use exportAsynchronouslyWithCompletionHandler:
when the app is backgrounded? 当应用程序后台运行时,是否可以使用exportAsynchronouslyWithCompletionHandler:
?
Edit 1 I've tested this while connected to the debugger and while not, the app never executes the copy command. 编辑1我已经在连接调试器时进行了测试,但没有测试过,该应用程序从不执行复制命令。 But does so only when the app is foregrounded again. 但是只有在再次显示应用程序时才这样做。
Edit 2 I posted a similar question about whether using NSFileManager's copyItemAtURL:toURL:error:
is a viable alternative (but I'm seeing the same behavior so don't think it is). 编辑2我张贴了一个类似的问题,关于是否使用NSFileManager的copyItemAtURL:toURL:error:
是可行的选择(但是我看到的是相同的行为,所以不要以为是)。
In general, if you need just a little time (up to a few minutes) to finish up some tasks even after the user leaves the app, you just request this from the OS. 通常,即使您只需要一点时间(最多几分钟)来完成某些任务(即使用户离开应用程序后),也只需向OS提出要求即可。 See the Executing Finite Length Tasks section in the Background Execution Chapter. 请参阅“后台执行”一章中的“ 执行有限长度任务”部分。 So, begin the background task when you call exportAsynchronouslyWithCompletionHandler
, and end it in the completion handler for that method. 因此,在调用exportAsynchronouslyWithCompletionHandler
时开始后台任务,并在该方法的完成处理程序中结束它。
If you are also using a background NSURLSession
. 如果您还使用后台NSURLSession
。 In that case, if the app is not in foreground when the tasks finish, it calls the app delegate's handleEventsForBackgroundURLSession
method, which passes a completionHandler
block. 在这种情况下,如果任务完成时应用程序不在前台,则它将调用应用程序委托的handleEventsForBackgroundURLSession
方法,该方法将传递一个completionHandler
块。 One must: 一个必须:
Save the completionHandler
provided to handleEventsForBackgroundURLSession
; 保存提供给handleEventsForBackgroundURLSession
的completionHandler
;
Instantiate the NSURLSession
with the same background identifier as the original background session; 使用与原始后台会话相同的后台标识符实例化NSURLSession
;
Let the session call the appropriate delegate methods for the completion of the tasks; 让会话调用适当的委托方法来完成任务; and 和
The session will call URLSessionDidFinishEventsForBackgroundURLSession
when they're all done, at which point you'd generally call the completionHandler
we originally received in the app delegate. 当会话全部完成后,该会话将调用URLSessionDidFinishEventsForBackgroundURLSession
这时通常将调用我们最初在应用程序委托中收到的completionHandler
。
In your case, you will want to defer the call to the saved completionHandler
until after all of the asynchronous exportAsynchronouslyWithCompletionHandler
handlers are done, too. 在您的情况下,您将希望将调用推迟到已保存的completionHandler
处理程序,直到所有异步exportAsynchronouslyWithCompletionHandler
处理程序也都完成之后。 There are bunch of ways you could do that (eg dispatch groups, etc.), but hopefully that illustrates the moving parts involved in this process. 您可以通过多种方式(例如调度组等)进行操作,但希望可以说明此过程中涉及的活动部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.