I have been working on an app in which user can record video using AVFoundation
and send to the server, video has maximum size up to 15M, depending on the internet speed & type it can take from 1 to 5 minutes approximately to transfer video to the server. I am transferring the recorded video to the server in the background thread so that user can continue other stuff on the app while video is being uploaded to the server.
While reading the Apple Docs for implementing long running tasks in backround, I see that only few kinds of apps are allowed to execute in the background.
e.g.
audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
Does it qualify my app also for running the tasks in the background? or I need to transfer the video on the main thread?
Scheduling deferred work through WorkManager is the best way to handle tasks that don't need to run immediately but which ought to remain scheduled when the app closes or the device restarts.
Use the BackgroundTasks framework to keep your app content up to date and run tasks requiring minutes to complete while your app is in the background. Longer tasks can optionally require a powered device and network connectivity. Register launch handlers for tasks when the app launches and schedule them as required.
When you hit the home button, an app moves from Active to Background and quickly to the Suspended state where it no longer uses CPU time or drains power. An app may request an additional 10 minutes (and not more than 10 minutes) of Background running to complete a big task before becoming Suspended.
For iOS Devices If Background refresh is greyed out in the ON position, go To Settings App - > General - > Background App Refresh - > Turn on the option for the system, and then you can turn on / off by app.
NSOperationQueue
is the recommended way to perform multi-threaded tasks to avoid blocking the main thread. Background thread is used for tasks that you want to perform while your application is inactive, like GPS indications or Audio streaming.
If your application is running in foreground, you don't need background thread at all.
For simple tasks, you can add a operation to a queue using a block:
NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperationWithBlock:^{
// Perform long-running tasks without blocking main thread
}];
More info about NSOperationQueue and how to use it.
The upload process will continue while in background, but your application will be eligible to be suspended, and thus the upload may cancel. To avoid it, you can add the following code to application delegate to tell the OS when the App is ready to be suspended:
- (void)applicationWillResignActive:(UIApplication *)application {
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Wait until the pending operations finish
[operationQueue waitUntilAllOperationsAreFinished];
[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
From your response to Dwayne, you do not need to be able to download in background mode. Rather what you need is to do your download in another thread (background thread) beside main thread. Something like this for GCD:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do you download here...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With