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:) ...

Get iOS Programming: The Big Nerd Ranch Guide, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.