
This video is only available to subscribers. Start a subscription today to get access to this and 470 other videos.
Updating Feeds in the Background
This episode is part of a series: Making a Podcast App From Scratch.
Background Update of Subscribed Feeds
As the user launches the application, we'll start by importing the new episodes of the subscribed feeds. We'll use a qualityOfService
of .background
.
private var priorityQueue: OperationQueue = {
let q = OperationQueue()
q.maxConcurrentOperationCount = 2
q.qualityOfService = .userInitiated
return q
}()
private var backgroundQueue: OperationQueue = {
let q = OperationQueue()
q.maxConcurrentOperationCount = 2
q.qualityOfService = .background
return q
}()
Updating the Podcast
As we fetch the feeds and import new episodes, we'll update the podcast by adding these episodes. We'll make sure this operation runs in the background and simultaneously logs the details of the subscribed podcast.
func updatePodcasts() {
backgroundQueue.addOperation {
let context = PersistenceManager.shared.newBackgroundContext()
let subscriptionStore = SubscriptionStore(context: context)
do {
let subs = try subscriptionStore.fetchSubscriptions()
for sub in subs {
guard let podcast = sub.podcast else { continue }
guard let id = podcast.id else { continue }
print("Queueing operation to update subscribed podcast: \(podcast.title ?? "?")")
let updateOperation = ImportEpisodesOperation(podcastId: id)
self.backgroundQueue.addOperation(updateOperation)
}
} catch {
print("Error fetching subscriptions for background update. \(error)")
}
}
}
As the application launches, we'll update the podcast on a background queue.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
Theme.apply(to: window!)
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
PersistenceManager.shared.initializeModel(then: {
FeedImporter.shared.startListening()
FeedImporter.shared.updatePodcasts()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.window?.rootViewController = storyboard.instantiateInitialViewController()
})
return true
}
Note that we've also bumped the minimum version of the app to iOS 13.