I'm getting inconsistent results when using the Background Tasks framework for my application written in SwiftUI. I'm only looking to make quick network requests, so I'm choosing to use BGAppRefreshTask.
Background fetch, and Background Processing are set in Signing & Capabilities. Permitted background task scheduler identifiers have been set. Manually calling it in debugging works fine on a real device but never in production.
I tested both BGAppRefreshTask, and BGProcessingTask. I noticed BGProcessingTask is being called but only when connected to a power supply. I never see any updates from BGAppRefreshTask. I'm not sure if I'm missing something simple.
BGAppRefreshTask hasn't run for FOUR days now since updating this post. BGProcessingTask was run 13 time's overnight but only if my device is charging. Even when setting requiresExternalPower to false.
BGAppRefreshTask run: 0 & BGProcessingTask run: 13
Calling in the debugger using commands here works but it's never run on my device without simulating in the debugger.
(lldb) e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:#"com.bgapp.refresh"]
2022-02-26 11:41:33.964753-0800 BGAppRefreshTask[9180:2203525] Simulating launch for task with identifier com.bgapp.refresh
2022-02-26 11:41:35.908739-0800 BGAppRefreshTask[9180:2203542] Starting simulated task: <decode: missing data>
2022-02-26 11:41:35.912108-0800 BGAppRefreshTask[9180:2203542] Marking simulated task complete: <BGAppRefreshTask: com.bgapp.refresh>
Received completion: finished.
UPDATE
I used getPendingTaskRequests to see if any task was being registered and it's apparent it is but still not executing. The earliest date is scheduled for 19:28 so 7:28PM. I registered my task at 11:23AM but it's not schedule to run for another 8 hours.
Pending task requests: [<BGAppRefreshTaskRequest: com.bgapp.refresh, earliestBeginDate: 2022-02-28 19:28:34 +0000>]
BGAppRefresh
/*!
BGAppRefreshTask
#abstract A background task used to update your app's contents in the background.
*/
class BGADelegate: NSObject, UIApplicationDelegate, ObservableObject {
let taskIdentifier = "com.bgapp.refresh"
#AppStorage("backgroundtask") var tasks: Int = 0
func application(_ applicatiown: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
register()
scheduleAppRefresh()
return true
}
func register() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
print("register")
}
}
func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: taskIdentifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
func handleAppRefresh(task: BGAppRefreshTask) {
scheduleAppRefresh()
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
DispatchQueue.main.async {
self.tasks += 1
}
// Network request here
task.setTaskCompleted(success: true)
print("handle app refresh")
}
}
BGProcessingTask
/*!
BGProcessingTask
#abstract A background task used to perform deferrable processing.
*/
class BGPDelegate: NSObject, UIApplicationDelegate, ObservableObject {
let taskIdentifier = "com.bgapp.refresh"
#AppStorage("backgroundtask") var tasks: Int = 0
func application(_ applicatiown: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
register()
scheduleAppRefresh()
return true
}
func register() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { task in
self.handleAppRefresh(task: task as! BGProcessingTask)
print("register")
}
}
func scheduleAppRefresh() {
let request = BGProcessingTaskRequest(identifier: taskIdentifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
request.requiresNetworkConnectivity = true
request.requiresExternalPower = false // Default value in false
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
func handleAppRefresh(task: BGProcessingTask) {
scheduleAppRefresh()
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
DispatchQueue.main.async {
self.backgroundtask += 1
}
// Network request here
task.setTaskCompleted(success: true)
print("handle app refresh")
}
}
So from new understanding of Background Tasks, I know now it's being scheduled for an earliest date but I was opening the application setting back the date it's scheduled for. I was not waiting past the earlier date scheduled when relaunching the application. Each task will be overwritten when setting the background app refresh task.
struct BGAppRefreshTaskApp: App {
#UIApplicationDelegateAdaptor var delegate: AppDelegate
#Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(delegate)
.onChange(of: scenePhase) { phase in
switch phase {
case .background:
delegate.scheduleAppRefresh()
delegate.background += 1
print("background")
case .active:
print("active")
BGTaskScheduler.shared.getPendingTaskRequests(completionHandler: { request in
print("Pending task requests: \(request)")
})
case .inactive:
print("inactive")
#unknown default:
break
}
}
}
}
}
Related
I am trying to setup some BackgroundTasks to run some code periodically (say once every day) while the app is closed. I believe the BackgroundTasks API on swift is just for that? (Please let me know if I'm mistaken). I followed the article here on Apple's Docs to implement it, and adjusting it to fit SwiftUI.
Problem: The background task never fires but is "pending" (as seen in the images)
Disclaimer: I did add the Background Modes capability and checked Background fetch and also Background processing, and added the identifer to the Info.plist
Code:
Main - Setup app delegate, get scene, set UserDefaults counter, button to get all pending tasks, text showing the UserDefault counter, Button to add to UserDefault counter, call the schedule() task when app enters background
import SwiftUI
import BackgroundTasks
#main
struct GyfterApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#Environment(\.scenePhase) var scene
#AppStorage("test") var test = 1
var body: some Scene {
WindowGroup {
VStack {
Button("Test") {
BGTaskScheduler.shared.getPendingTaskRequests { all in
print("Pending Tasks Requests", all)
}
}
Text("\(test)")
Button("ADD") {
test = test + 1
}
}
.onChange(of: scene) { newValue in
switch newValue {
case .background:
print("Entered Background")
appDelegate.schedule()
default:
break
}
}
}
}
}
Operation - Operation for the BackgroundTasks, it prints a message and adds 1 to the UserDefaults counter
class OP: Operation {
#AppStorage("test") var test = 1
override func main() {
print("OPERATION RAN")
test = test + 1
}
}
AppDelegate -
Register task with given identifier, print message when it runs, and print the output of the register call to see if it was registered (it gets registered but doest not call handleSchedule(task:))
Schedule the task request with identifier, for 60 seconds ahead, submit to the scheduler (schedule() only gets called when app enters background, not when handleSchedule(task:) is supposed to fire which never gets called)
handleSchedule(task:) print statement, call schedule(), create OperationQueue, create Operation, set expirationHandler and setTaskCompleted, add operation to the queue
class AppDelegate: NSObject, UIApplicationDelegate {
#AppStorage("test") var test = 1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("LAUNCHED")
let a = BGTaskScheduler.shared.register(forTaskWithIdentifier: "IDENTIFIER", using: nil) { task in
print("REGISTERED")
self.test = self.test + 1
self.handleSchedule(task: task as! BGAppRefreshTask)
}
print(a)
return true
}
func schedule() {
let request = BGAppRefreshTaskRequest(identifier: "IDENTIFIER")
request.earliestBeginDate = Date(timeIntervalSinceNow: 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
func handleSchedule(task: BGAppRefreshTask) {
print("HANDLING SCHEDULE")
schedule()
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
let operation = OP()
task.expirationHandler = {
print("BG Task Expired")
queue.cancelAllOperations()
}
operation.completionBlock = {
print("OPERATION COMPLETED")
task.setTaskCompleted(success: !operation.isCancelled)
}
queue.addOperation(operation)
}
}
Console log: Display of the print statements from the function calls
Line 1: App launched
Line 2: BGTaskScheduler "registered" (supposedly) the task with identifier
Line 3: Array of the pending tasks
Line 4: App entered background (and schedule() got called)
Line 5: Array of the pending tasks
LAUNCHED
true
Pending Tasks Requests []
Entered Background
Pending Tasks Requests [<BGAppRefreshTaskRequest: INDENTIFIER, earliestBeginDate: 2022-02-28 02:57:50 +0000>]
This code is supposed to regularly schedule a background task.
Running on a device, I never ever see the "background refresh started" log that would prove the task has been scheduled by the system.
Everything else runs fine.
When I use the "force background task launch trick" by typing e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:#"TaskIdentifier"] in the console I do see the task being executed.
I have Background fetch and Background Processing activated in the target capabilities.
So why is the system not schedule the task ?
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
MSLogger.debug("Application Event : didFinishLaunchingWithOptions")
// Register the background refresh handler and schedule it
registerForBackgroundRefresh()
scheduleNextRefresh()
return true
}
//------------------------------------------------------------------------------------------------------------------
// MARK: - Background refresh management
let backgroundTaskIdentifier = "TaskIdentifier"
func registerForBackgroundRefresh() {
let crd = BGTaskScheduler.shared.register(forTaskWithIdentifier: backgroundTaskIdentifier,
using: nil) {
task in
self.handleBackgroundRefresh(task: task as! BGAppRefreshTask)
}
MSLogger.debug("task registration : \(crd)")
}
func scheduleNextRefresh() {
// Schedule the next refresh for within 2 hours
let request = BGAppRefreshTaskRequest(identifier: backgroundTaskIdentifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: 30 /*2 * 3600##*/)
do {
try BGTaskScheduler.shared.submit(request)
MSLogger.debug("submit background task request")
} catch {
MSLogger.debug("Could not schedule app refresh: \(error.localizedDescription)")
}
}
func handleBackgroundRefresh(task: BGAppRefreshTask) {
MSLogger.debug("background refresh started")
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
let operation = BlockOperation {
// Do something
}
task.expirationHandler = {
// After all operations are cancelled, the completion block below is called to set the task to complete.
queue.cancelAllOperations()
self.scheduleNextRefresh()
}
operation.completionBlock = {
task.setTaskCompleted(success: operation.isCancelled)
self.scheduleNextRefresh()
}
queue.addOperation(operation)
}
}
I'm trying to check some HealthKit data in the background and send a local notification if certain criteria were met.
I've added Background fetch capability and added my task's id to info.plist.
here is the background task code:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// other setups
self.setupHealthKit()
self.setupBackgroundTasks()
return true
}
fileprivate func setupHealthKit() {
HealthKitHelper.shared.executeObserverQuery(for: HealthKitTypes.activeEnergy,
predicate: nil)
.sink(receiveCompletion: {
completion in
switch completion {
case .failure(let error):
print("observer query execution failed with error: \(error.localizedDescription)")
EventLogger.BackgroundDelivery.logObserverQueryError(error: error.localizedDescription)
case .finished:
print("observer query executed.")
}
}, receiveValue: {
query, handler in
// we have some updates
EventLogger.BackgroundDelivery.logObserverQueryResultInBackground()
// check if there is a new sleep session
let queue = OperationQueue()
let operations = self.getBackgroundOperations()
let last = operations.last!
last.completionBlock = {
handler()
}
queue.addOperations(operations, waitUntilFinished: false)
})
.store(in: &self.subscriberStore)
HealthKitHelper.shared.enableBackgroundDelivery(for: HealthKitTypes.activeEnergy,
frequency: .hourly)
.sink(receiveCompletion: {
completion in
switch completion {
case .failure(let error):
print("failed to enable bg delivery with error: \(error.localizedDescription)")
case .finished:
print("delivery enable finished.")
}
}, receiveValue: {
status in
print("delivery enable success status: \(status)")
})
.store(in: &self.subscriberStore)
}
func setupBackgroundTasks() {
let status = BGTaskScheduler.shared.register(forTaskWithIdentifier: self.BACKGROUND_FETCH_TASK_ID,
using: nil) {
task in
self.handleBackgroundAppRefresh(task as! BGAppRefreshTask)
}
}
I'm just sending a local notification in my background task to see if it works.
now I'm able to run the background task in the debugger, but it never gets executed by the system.
And I also don't know what's the best way to get new health kit samples in the background.
You have to use HKObserverQuery to query health data in the background.
I am using the new iOS13 background task framework, with the implementation of a BGAppRefreshTask type. My problem is, my device is never calling the task, even after waiting several hours, but I am able to successfully run the code using the Debugger trick of calling _simulateLaunchForTaskWithIdentifier.
I have setup the following:
Enabled my app with the Background Modes capability, a checking the "Background fetch".
Added my background Id to Info.plist under "Permitted background task scheduler identifiers": "com.XYZ.PearWeather.backgroundAlerts".
I have registered the task from application(didFinishLaunchingWithOptions) in my AppDelegate:
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.XYZ.PearWeather.backgroundAlerts", using: nil) { task in
self.backgroundAlerts(task: task as! BGAppRefreshTask)
}
I am scheduling the task in a func within the AppDelegate, and calling it from my SceneDelegate sceneDidEnterBackground(). It was originally a static func, but I have now changed it to an instance func, and getting the AppDelegate instance (since I have tried many changes in desperation):
func sceneDidEnterBackground(_ scene: UIScene) {
(UIApplication.shared.delegate as! AppDelegate).scheduleBackgroundAlerts()
}
func scheduleBackgroundAlerts() {
let request = BGAppRefreshTaskRequest(identifier: "com.XYZ.PearWeather.backgroundAlerts")
request.earliestBeginDate = Date(timeIntervalSinceNow: 5 * 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
At least in the Debugger scenario, there is no error in the submit call. I have tried many different values for the timeIntervalSinceNow parameter above. I am also calling this scheduleBackgroundAlerts() func from the task handler itself, which is as follows:
func backgroundAlerts(task: BGAppRefreshTask) {
scheduleBackgroundAlerts()
task.expirationHandler = {
// After all operations are cancelled, the completion block below is called to set the task to complete.
task.setTaskCompleted(success: false)
}
AlertsOperation.showNotification()
task.setTaskCompleted(success: true)
}
This implementation has changed a lot - I have originally used an OperationQueue, tried placing the scheduleBackgroundAlerts() call at the beginning and end of the func, etc. Now it is stripped down. The AlertOperation.showNotification() is also very simple now:
static func showNotification() {
let now = Date()
let bg = Locale.currentLocale().formattedTime(date: now)
SettingsManager.shared.settings.bg = bg
}
This is just storing a value in UserDefaults (in my SettingsManager, details of which are not relevant here) that I am able to read back in my app to see if anything happened.
Now, the original implementation of this func issues a local notification using UNUserNotificationCenter etc, which is what I am trying to do in this background task. This worked fine from the Debugger, but I reduced it to this simple code just to make a very small implementation.
As I say, calling the task handler from the Debugger works fine, using:
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:#"com.XYZ.PearWeather.backgroundAlerts"]
But nothing is happening from the device itself. I cannot see what I have missed. I do not know how to log any exception from within the background task handler either.
I am new to Swift and iOS, so any pointers appreciated. Most of the code above is almost a copy of the many tutorials on this subject. For me, though, things are not working and I have run out of options!
have break points in the code after submit request and after submitting the request(BGTaskScheduler.shared.submit(request)) then run the command
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:#"com.XYZ.PearWeather.backgroundAlerts"]
then registered handler will be called for that identifier.
Check POC to see further details.
Here is what I use.
In my AppDelegate, the iOS13 code. Older style fetch not included:
class AppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate, UNUserNotificationCenterDelegate {
var backgroundSessionCompletionHandler: (() -> Void)?
var backgroundSynchTask: UIBackgroundTaskIdentifier = .invalid
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.WH.Air-Compare.WayneBGrefresh", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
self.logSB.verbose("iOS 13 Registered for Background duty")
}
} else {
// Fallback on earlier versions
self.logSB.verbose("iOS < 13 Registering for Background duty")
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
}
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler:#escaping () -> Void) {
let dateFormatter = DateFormatter() // format the date for output
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.long
let convertedDate = dateFormatter.string(from: Date())
self.logSB.info("Background URLSession handled at \(convertedDate)")
self.logSB.info("Background URLSession ID \(identifier)")
let config = URLSessionConfiguration.background(withIdentifier: "WayneBGconfig")
let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) -> Void in
// yay! you have your tasks!
self.logSB.info("Background completion handler here with \(downloadTasks.count) tasks")
for i in 0...max(0,downloadTasks.count - 1) {
let description: String = downloadTasks[i].taskDescription!
self.logSB.info("Task ID \(i) is \(description)")
}
}
backgroundSessionCompletionHandler = completionHandler
}
func applicationDidEnterBackground(_ application: UIApplication) {
if #available(iOS 13.0, *) {
scheduleAppRefresh()
} else {
// Fallback on earlier versions
}
}
#available(iOS 13.0, *)
func scheduleAppRefresh() {
logSB.info("Scheduling the AppRefresh in iOS 13 !!")
let request = BGAppRefreshTaskRequest(identifier: "com.WH.Air-Compare.WayneBGrefresh")
request.earliestBeginDate = Date(timeIntervalSinceNow: 2 * 60) // Fetch no earlier than 2 minutes from now
do {
try BGTaskScheduler.shared.submit(request)
} catch {
logSB.error("Could not schedule app refresh: \(error)")
}
}
I'm trying to use applicationWillResignActive() in order to sync some data to my Firestore database before the application enters the background.
func applicationWillResignActive(_ application: UIApplication) {
self.uploadWantToPlay()
}
When I call my upload function from applicationWillResignActive() it runs but no data is added to Firestore before the next time the application becomes active.
When I for testing purposes instead run the same function from one of my ViewControllers the data is added instantly to Firestore.
I've also tried calling the function from applicationDidEnterBackground(), I've tried running it in it's own DispatchQueue. But it's had the same result.
How can I run this function as the user is about to leave the app and have it perform the database sync properly?
The functions handling the database sync;
func uploadWantToPlay() {
print ("Inside uploadWantToPlay")
if let wantToPlay = User.active.wantToPlayList {
if let listEntries = wantToPlay.list_entries {
let cleanedEntries = listEntries.compactMap({ (entry: ListEntry) -> ListEntry? in
if entry.game?.first_release_date != nil {
return entry
} else {
return nil
}
})
let gamesToUpload = cleanedEntries.filter {
$0.game!.first_release_date! > Int64(NSDate().timeIntervalSince1970 * 1000)
}
DatabaseConnection().writeWantToPlayToDatabase(user: User.active,wantToPlay: gamesToUpload)
}
}
}
func writeWantToPlayToDatabase(user: User, wantToPlay: [ListEntry]) {
firebaseSignIn()
let deviceId = ["\(user.deviceId)": "Device ID"]
for entry in wantToPlay {
let wantToPlayGameRef = fireStore.collection(WANTTOPLAY).document("\(entry.game!.id!)")
wantToPlayGameRef.updateData(deviceId) {(err) in
if err != nil {
wantToPlayGameRef.setData(deviceId) {(err) in
if let err = err {
Events().logException(withError: err, withMsg: "DatabaseConnection-writeWantToPlayToDatabase(user, [ListEntry]) Failed to write to database")
} else {
print("Document successfully written to WantToPlayGames")
}
}
} else {
print("Document successfully updated in WantToPlayGames")
}
}
}
}
According to the Apple documentation
Apps moving to the background are expected to put themselves into a
quiescent state as quickly as possible so that they can be suspended
by the system. If your app is in the middle of a task and needs a
little extra time to complete that task, it can call the
beginBackgroundTaskWithName:expirationHandler: or
beginBackgroundTaskWithExpirationHandler: method of the UIApplication
object to request some additional execution time. Calling either of
these methods delays the suspension of your app temporarily, giving it
a little extra time to finish its work. Upon completion of that work,
your app must call the endBackgroundTask: method to let the system
know that it is finished and can be suspended.
So, what you need to do here is to perform a finite length task while your app is being suspended. This will buy your app enough time to sync your records to the server.
An example snippet:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var backgroundTask: UIBackgroundTaskIdentifier!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
self.registerBackgroundTask()
// Do your background work here
print("Do your background work here")
// end the task when work is completed
self.endBackgroundTask()
}
func registerBackgroundTask() {
self.backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.endBackgroundTask()
}
assert(self.backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
print("Background task ended.")
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
}
For further information refer to this article.