Mastering In CoreData (Part 15 Multithreading Concurrency Strategy Parent — Child Use Case 1)
Solved Real Problem Using Parent-Child Context
A managed object context is an in-memory scratchpad for working with your managed objects.We used multiple managed object context for performing two type of tasks.
1) For Long running tasks as discussed in previous parts. In this part we will do this in real application
2) In other situations, such as when edits are being made to user data, it’s helpful to treat a managed object context as a set of changes that the app can discard if it no longer needs them. Using child contexts makes this possible. In next part we will cover this
In this tutorial, you’ll used multiple managed object contexts by taking a GooglePlayViewerApp app and improving it in several ways by adding multiple contexts.
This tutorial starter project is a google play viewer app for the demonstration purpose.When application first launch you will redirect to login screen as shown in Figure 1. When you tap on Export it will load 47472 google play apps data into Core Data.
On tap in Login button it will redirect you to listing screen where it will show all the apps name as shown in Figure 2
In Figure 3 project structure screen shot is shown
ViewController → Responsible for login screen task. It has actions method for the export and login button tapped. When tapped on login button it will redirect to listing screen. On export button tapped it will load data from csv file into core data and saved it to the persistent store.
googleplaystore.csv → Contains all google play apps information which is total of 47472 as shown in Figure 4.
GooglePlayListViewController → Responsible for display list of google play apps from. It fetches data from core data and display it in tableview.
AppDelegate → Responsible for creating Core data stack.
GooglePlayViewerApp.xcdatamodeld → Contains Google play entity schema and configuration
GooglePlayViewerApp.xcdatamodeld → Select Google Play Entity as shown in Figure 5 contains all the properties needed by Google Play csv data
Observation / Problem
Make sure you delete the application first. Run the application and the login screen will appear. Tap on textfield and start typing and it is working fine. Now tap on export button top left on the navigation bar and immediately start typing again you will see the UI is hanging.
The export operation takes several seconds, and it prevents the UI from responding to interact events such as typing.
What is happening
As shown in Figure 7 Since this application was using only One Managed Object Context which was populating csv data into Core Data on main thread. When you tapped on export button following things will happened
- Loaded all the contents of the csv file which contain 47472 data into
datavariable in main thread
- Get the managed object context reference which was created on main thread
- Created GooglePlay CoreData Entity on main context and populate its properties as well
- Pushed data to persistent store
As you can see in the Figure 7 console logs approx 6 sec application main thread will block by this task and you can imagine what will be the user reaction when your application freezed almost 6 seconds
Improve Performance Using Concurrency
As you can see we reduces the block time of main thread from 6 secs to 0.5 secs. As you can see in Figure 8 we did number of things
- Created parent-child configuration we learned in previous part in which parent is the main thread Managed Object Context which was populating listing screen when we tap on login. It’s responsibility to interact only UI related task
perform:method on child context to switch the thread where this was created. Note: Don’t call
performAndWait:since it will run serially and also idle the main thread.
- Content loading from CSV and Creation of Core Data objects was done on Private Queue / thread and the context for Core Data objects was child context which wass private
- After heavy processing we merged changes on main thread context (parent). This merging will done on main thread. In this time which is 0.5 sec, main thread will be busy.
By using parent child concurrency strategy we get the huge performance gains.
As we said earlier we used multiple managed object context for performing two type of tasks. One task was to move Long running tasks into some background thread. In this part we achieved it using parent — child configuration
For Long running tasks (To achieve Concurrency)
In the next part we will look how we used parent — child configuration to solved temporary changes problem happened in the context