Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Ignored request to start a new background task because RunningBoard has already started the expiration timer

Tags:

swift

I have a series of tasks that is triggered by a silent push notification. Upon receiving the push notification, it wakes the iOS up in the background and performs the following tasks:

  1. Opens up a WebViewController that contains a WKWebview
  2. Goes to a webpage, and clicks some buttons automated by javascript injection
  3. Once completed, dismisses the WebViewController

I have added selected BackgroundTasks handlers to manage it by following this tutorial but the console is flooded with the following warning.

[ProcessSuspension] 0x280486080 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because RunningBoard has already started the expiration timer

Note that the tasks that needs to be done are still performed correctly.

class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {

    lazy var webView: WKWebView = {
        let v = WKWebView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.navigationDelegate = self
        return v
    }()
    
    var backgroundTask: UIBackgroundTaskIdentifier = .invalid
    
    //Remove BG task when not needed
    deinit {
        NotificationCenter.default.removeObserver(self)
        endBackgroundTask()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Register notification for background task
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(reinstateBackgroundTask),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)
        registerBackgroundTask()

        
        //Load webview with  URL
        if let url = url {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }
    
    //MARK:- Handle BG Tasks
    
    func registerBackgroundTask() {
        backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
            self?.endBackgroundTask()
        }
    }
    
    func endBackgroundTask() {
        Log("Background task ended.")
        UIApplication.shared.endBackgroundTask(backgroundTask)
        backgroundTask = .invalid
    }
    
    @objc func reinstateBackgroundTask() {
        if backgroundTask == .invalid {
            registerBackgroundTask()
        }
    }
    
    func endBackgroundTaskIfNotInvalid() {
        if backgroundTask != .invalid {
            self.endBackgroundTask()
        }
    }
    
    //This is the final task that needs to be done 
    fileprivate func updateScheduler(visitedPlace: VisitedPlace) {
        if navigator == .scheduler {
            
            if let jobId = jobId {
                let data = [
                    "status": "scheduled",
                    "completedOn": Date()
                ] as [String : Any]
                
                ///Do some work here...
                    
                //Dismiss controller after completing
                self.dismiss(animated: true) {
                    self.endBackgroundTaskIfNotInvalid()
                }
            }
        } else {
            self.endBackgroundTaskIfNotInvalid()
        }
    }
}

What is triggering all these warnings and how do I silence it?

like image 560
Koh Avatar asked Dec 12 '25 03:12

Koh


2 Answers

I'm having the same console flood. For me it turned out to be adMob that was the cause.

like image 169
mark Avatar answered Dec 15 '25 13:12

mark


This happens to me when I run unit tests that wait for test expectations to be filled. I was hoping that it was just a simulator issue, since I don't see it in production, but it sounds like that's not the case.

like image 34
NRitH Avatar answered Dec 15 '25 11:12

NRitH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!