Background Tasks
You learned in Chapter 20 that blocking the main queue can lead to an unresponsive application, so it is often a good idea to do expensive operations using a background task. The viewContext of NSPersistentContainer is associated with the main queue, so long-running operations using the viewContext – like the insertion of photos from the web service – are good candidates for a background task.
You are going to update processPhotosRequest(data:error:) to use a background task. Background tasks are an asynchronous operation, so instead of returning a Result synchronously from your method, you will pass a completion handler to be called asynchronously.
Open PhotoStore.swift and update processPhotosRequest(data:error:) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access