Background processing question

Hi,

I have a hard time getting my head wrapped around the possibilities of running a app or a task in a app in the background. I have a app where I utilize MusicKit to create a playlist in Apple Music, and add songs to the playlist. Now the songs added are picked from choices made by the user, and the total number of songs to add is 75, and that takes some time. And if the user switches to a different app or the phone is locked, the add songs logic stops, and then starts again as soon as the app is active again. What I am trying to achieve is of course for this to keep processing also when the app is not active, so basically to keep it running in the background. But this is where I struggle to understand how I can do that - The available choice seems to be BGTaskScheduler, but that just does not seem correct. From what I understand it just schedules a task, and it will be processed whenever the app or phone "feels like it" (again, my understanding, might be wrong), and that won't work in my scenario. I want the task to start when the user taps a button, and just keep running until it is finished, regardless of if the app is active or not.

Any pointers, tips, advices out there on how I can achieve this?

Answered by DTS Engineer in 832433022

Any pointers, tips, advices out there on how I can achieve this?

So, the main API your missing are beginBackgroundTask(withName:expirationHandler:)*/ performExpiringActivity(withReason:using:). Both of them do the same thing, which is to give you an additional ~30s after your app enters the background to finish doing whatever you need to finish. See Quinn's "UIApplication Background Task Notes" for more some additional guidance on how to use these APIs

*Note that the term "background task" here is totally unrelated to the "BackgroundTask" framework.

Unfortunately, that's basically the only API option you currently have for extending your execution time in the way you're describing.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Accepted Answer

Any pointers, tips, advices out there on how I can achieve this?

So, the main API your missing are beginBackgroundTask(withName:expirationHandler:)*/ performExpiringActivity(withReason:using:). Both of them do the same thing, which is to give you an additional ~30s after your app enters the background to finish doing whatever you need to finish. See Quinn's "UIApplication Background Task Notes" for more some additional guidance on how to use these APIs

*Note that the term "background task" here is totally unrelated to the "BackgroundTask" framework.

Unfortunately, that's basically the only API option you currently have for extending your execution time in the way you're describing.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Thank you so much for your reply! I tested the beginBackgroundTask in my app now, and it works just as you say, it gives the app about 30 seconds of running time after the app enters background. That of course helps, although it is unfortunately not enough to finish the task of adding that number of songs to a playlist. I guess I just have to look into switching to using the Apple Music API instead of MusicKit, which from what I have understood accepts batch loads of songs to a playlist instead of one by one, which should make it a lot quicker and able to finish at least within those 30 seconds given by using beginBackgroundTask.

Background processing question
 
 
Q