Runloop always cause XPC connection interrupted?

1k views Asked by At

Hi I notice if I add runloop when app enter background will cause XPC connection interrupted

for example , my app connection to a BLE device , if user is let the app enter background for a while , I will exit the app than release the connection

Here is my code

func applicationDidEnterBackground(_ application: UIApplication) {
        isInBackground = true
        timer = Timer.scheduledTimer(timeInterval: 300 , target: self, selector: #selector(self.quitApp), userInfo: nil, repeats: false)
        RunLoop.current.add(timer, forMode: .commonModes)
        RunLoop.current.run()
    }

func quitApp() {
    if isInBackground == true {
        print("QUIT APP")
        exit(0)
    }
}

func applicationWillEnterForeground(_ application: UIApplication) {
        timer.invalidate()
        isInBackground = false
}

But every time I enter foreground , I found if I remove the runloop in

func applicationDidEnterBackground

The app will run

func applicationDidBecomeActive 
or 
func applicationWillEnterForeground

But if I add the Runloop , it will cause

XPC connection interrupted

I don't understand what's relation between Runloop and app life cycle ???

Also , if I let the app enter background enough time , app will exit , than open app again everything is fine .

****** Update ******

Here is the final code I use , and it works fine and no crash But not sure it's because I modify my project setting or not ... It was an issue long time ago

func applicationDidEnterBackground(_ application: UIApplication) {

    //Quit if Enter background 5 min
    isInBackground = true
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(quitApp), userInfo: nil, repeats: true)
    timer.fire()
    RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)
    let app = UIApplication.shared
    var bgTask:UIBackgroundTaskIdentifier? = UIBackgroundTaskInvalid
    bgTask = app.beginBackgroundTask{ () -> Void in
        DispatchQueue.main.async{
            if bgTask != UIBackgroundTaskInvalid{
                bgTask = UIBackgroundTaskInvalid
            }
        }
    }
}

func applicationDidBecomeActive(_ application: UIApplication) {
    timer.invalidate()
    isInBackground = false
    backgroundCount = 0
}

@objc func quitApp() {
    if backgroundCount < 300 {
        backgroundCount += 1
    }else {
        if isInBackground == true {
            exit(0)
        }
    }
}
1

There are 1 answers

1
Charles Srstka On

XPC is implemented using a run loop, so it's not too surprising that tampering with the run loop would interrupt your XPC connections.