I'm working on an iOS app, uploading videos from the Camera Roll, using NSURLSession with a background configuration. The user can queue up multiple videos for upload (the queue is executed serially).
A single upload consists of:
- Getting an AVURLAsset reference to the PHAsset using PHImageManager's
requestAVAssetForVideo
method. - Copying the resource to a temp directory (because you cannot upload straight from the AVURLAsset's URL).
- Uploading the resource using an NSURLSessionUploadTask
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 copyItemAtURL:toURL:error:
stage it stalls until I foreground the app again. (I know this because I'm posting debug statements in local notifications, visible on the lock screen).
Is it possible to use copyItemAtURL:toURL:error:
when the app is backgrounded?
If not, is it possible to use an AVAssetExportSession instead?
Edit 1 I've tested this while connected to the debugger and while not, the app never executes the copy command. But does so only when the app is foregrounded again.
Edit 2
To clarify, execution stalls at the copy command. It doesn't yield an error and continue execution. And implementing the fileManager:shouldCopyItemAtURL:toURL:
delegate method doesn't change things. The docs for that method also say:
Prior to copying each item, the file manager asks its delegate if it should actually do so. It does this by calling the fileManager:shouldCopyItemAtURL:toURL: method; if that method is not implemented (or the process is running in OS X 10.5 or earlier) it calls the fileManager:shouldCopyItemAtPath:toPath: method instead. If the delegate method returns YES, or if the delegate does not implement the appropriate methods, the file manager proceeds to copy the file or directory.