While swift

how can I stop the while loop when the app again enters foreground

func applicationDidEnterBackground(_ application: UIApplication) {
        var finished = false
        var bgTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0);
        bgTask = application.beginBackgroundTask(withName:"MyBackgroundTask", expirationHandler: {() -> Void in
            // Time is up.
            if bgTask != UIBackgroundTaskIdentifier.invalid {
                // Do something to stop our background task or the app will be killed
                finished = true
        // Perform your background task here
        print("The task has started")
        if finished == false {
            while !finished {
                print("running") }
        // Indicate that it is complete
        bgTask = UIBackgroundTaskIdentifier.invalid

The approach you’ve outlined indicates that you’re not quite grokking the whole background task thing. As this is very specific to iOS, my recommendation is that you post your question to App Frameworks > iOS Multitasking on DevForums and I can advise you in that context.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple