How can stop while?

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
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")

    while !finished {
        if( finished == false ){
           print( "no finished")
        }else {
            finished = true
            print( "finished")

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    finished = true

Can you edit your post to put all the code in the single code block with the indentation matching the original source? The current formatting means I’m not sure if I’m reading the code correctly.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Terms of Service

Privacy Policy

Cookie Policy