I’m making an attempt to setup this background activity for my app that runs repeatedly each 5 minutes. The duty get’s scheduled however the activity itself by no means runs. I additionally by no means see the duty registered.
App Delegate:
import UIKit
import BackgroundTasks
@essential
class AppDelegate: UIResponder, UIApplicationDelegate {
func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override level for personalization after utility launch.
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.Storyboard.backgroundTaskIdentifier, utilizing: nil) { activity in
self.handleBackgroundTask(activity: activity as! BGAppRefreshTask)
print("activity registered")
}
UIBarButtonItem.look().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont(name: "Avenir Next Bold", size: 18)! ], for: .regular)
return true
}
// MARK: UISceneSession Lifecycle
func utility(_ utility: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, choices: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Referred to as when a brand new scene session is being created.
// Use this technique to pick out a configuration to create the brand new scene with.
return UISceneConfiguration(title: "Default Configuration", sessionRole: connectingSceneSession.position)
}
func utility(_ utility: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Referred to as when the consumer discards a scene session.
// If any periods had been discarded whereas the appliance was not working, this will probably be known as shortly after utility:didFinishLaunchingWithOptions.
// Use this technique to launch any sources that had been particular to the discarded scenes, as they won't return.
}
func handleBackgroundTask(activity: BGAppRefreshTask) {
activity.expirationHandler = {
// Deal with expiration if mandatory
print("activity expired")
}
print("beginning activity")
// Carry out the background activity
BackgroundTaskManager.performBackgroundTask { lead to
activity.setTaskCompleted(success: outcome == .newData)
scheduleBackgroundTask()
}
}
// Different AppDelegate strategies...
}
func scheduleBackgroundTask() {
print("Process Scheduled")
let request = BGAppRefreshTaskRequest(identifier: Constants.Storyboard.backgroundTaskIdentifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: 10) // 5 minutes
do {
attempt BGTaskScheduler.shared.submit(request)
} catch {
print("Did not schedule background activity: (error)")
}
}
I even have the scheduleBackgroundTask()
operate set to run within the residence display screen and I’ve set it to run each 10 seconds for testing functions.
That is what the duty does at the moment as a check.
class BackgroundTaskManager {
static func performBackgroundTask(completion: @escaping (UIBackgroundFetchResult) -> Void) {
// Make API name and fetch new information
print("Background Process")
// Examine new information with previous information
// if newDataIsDifferent {
// Ship an area notification with adjustments
// Use UNUserNotificationCenter to ship a notification
// }
// Point out success or failure to the system
completion(.newData) // or .noData or .failed
}
}