Visualizing multiple threads

The following diagram will help you understand multiple threads:

When saveMovie(withName:) is called, the execution is still on the main thread. The persistence block is opened, the movie is created, its name is set, a helper is created, and then fetchRating(forMovie:callback:) is called on the helper. This call itself is still on the main thread. However, the fetching of data is pushed to a background thread. This was discussed earlier when you experimented with fetching data in a playground.

The callback that's invoked by dataTask is called on the same background thread that the task itself is on. The code will do its thing with the JSON and finally, the callback that was passed to fetchRating(forMovie:callback:) is called. The code inside of this callback is executed on the background thread as well.

You can see that the set movie-rating step in the update flow is somehow pushed back to the main thread. This is because of the persist method that you added as an extension to the managed object context. The context uses the perform method internally to ensure that any code we execute inside of the persist block is executed on the thread the managed object context is on. Also, since the managed object context was created on the main thread, the movie rating will be set on the main thread.

If you didn't set the movie rating on the same thread that the managed object belongs to, you would get errors and undefined behavior. Always make sure that you manipulate Core Data objects on the same thread as their managed object context.

Threading is a complex subject, but it's essential for building responsive applications. Network logic is a great example of why multithreading is important. If we didn't perform the networking on a separate thread, the interface would be unresponsive for the duration of the request. If you have other operations that might take a while in your app, consider moving them onto a background thread so they don't block the user interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset