Background Modes Tutorial: Getting Started | Ray Wenderlich

In this tutorial, you’ll create an app that uses the most common background modes: audio playback, location updates, general tasks, and background fetch.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5817-background-modes-tutorial-getting-started

Thanks for your hard work updating to Swift 4. Just read this morning, the tutorial was still in Swift 3 but now they got updated to Swift 4!

What I needed to learn from this article was about receiving location information with app operating in the background, so thanks for this article. I am developing a fitness app that keeps track of your running route and calculating metrics as you run. (e.g. calculating speed, total distance covered and etc) I was wondering if all the functions I have written that will process the data to calculate stats must be subjected to UIBackgroundTaskIdentifier and such? I tried my app in the background and it worked just as I planned it but I wondered if I had to do something extra to ensure I let iOS know I am done with that functions that ran in the background?

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

UIBackgroundTaskIdentifier is there to let iOS know you still need to do work even though your app is in the background. You call registerBackgroundTask to begin the background task. Inside that method, you run UIApplication.shared.beginBackgroundTask. To end the background task you call endBackgroundTask. If you didn’t call the ending function, iOS would run the code inside the closure in registerBackgroundTask when you were out of time and then terminate the task. In this tutorial, the closure runs the endBackgroundTask function. Technically you don’t have to do anything to end the background task but it’s good practice to explicitly end it when you’re done. If you didn’t and ignored the signal to end the task, iOS would terminate your app entirely.

//2
    for viewController in viewControllers {
      if let fetchViewController = viewController as? FetchViewController {
        //3
        fetchViewController.fetch {
          //4
          fetchViewController.updateUI()
          completionHandler(.newData)
        }
      }
    }

Could fetchViewController would be retained memory by itself in this code snippet (The problem about retain cycle in block case)?

@wviper3 Do you have any feedback about this? Thank you - much appreciated! :]

There won’t be any retain cycle in this case: fetchViewController is a local constant and as soon the execution leaves the scope of the method it won’t hold a reference to our constant. It will only live until the closure in this fetch method parameter finishes (calls updateUI and the escaping completionHandler). After that, the fetch method parameter’s closure goes out of the memory and there is nothing more to keep our local fetchViewController constant alive. (However, if the app is still active, the FetchViewController instance will be retained in by the TabBarController in the view hierarchy)

(@wviper3: please correct me if I’m wrong!)

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

I have set startMonitoringSignificantLocationChanges in my Location Manager settings that gives a Location Update trigger even when my application is terminated.

Is there any background mode in which I can run use any reminder to to fetch interval based location changes when my application is terminated.

Hi Abdul,

I am also using startMonitoringSignificantLocationChanges, it does give location update even though if app is terminated but i am not getting very accuracy with the help of this function.
Please reply if you get any solution for this. It seems we are facing same issue.

Regards

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

I have tried the Demo Project for Location Update in the Background Mode. It works great but only upto 15 to 20 minutes. Then it stopped updating location in the Background. Do I need any additional setup to perform Background location tracking for long hours without interruptions? Does anyone have any idea? Please help! Thanks :slight_smile:

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!