Nested Parent Child Context Relationship
Intermediate Training Core Data
For today's lesson, we'll learn about how to very easily setup Core Data Contexts so that we can merge in small changes. Let's first discuss what a Nested Parent Child Context Relationship is and how changes will propagate upwards to a parent context. Next, we write the code that constructs a private concurrency context ourselves, which will then allow us to set the parent to the main context. Finally, we'll have to execute a save on the main context to make sure changes are persisted all the way into Core Data. If we fail to do this, all changes are discarded upon restart of our app.

Comments (8)
iHobbit
5 years ago
So the changes show up in the UI immediately because the companies[] array already has a reference to the entity you modify. Is my understanding correct that if you added a new company in the child context, you would have either manually add that new object to the companies array or refetch for the new row to appear in the table?
Brian Voong
5 years ago
Tube
5 years ago
Why in the World doesn't Core Data check .hasChanges on save?
Brian Voong
5 years ago
Rupali Ghate
5 years ago
Can privateContext.parent be used for saving propagated changes in viewContext instead of accessing viewContext from persistenceContainer again?
Brian Voong
5 years ago
Tùng Vux
4 years ago
Why when we use private context, we just need call tableView.reloadData instead of re-fetch data as we do in doUpdates() or doWork() ?
Brian Voong
4 years ago
overcome
4 years ago
Hello! I'm using the same objects with multiple process for example: (Show in the UI, Create contacts, and Donwload Images.) at the same time, and I use child context with performBlockAndWait to try have fresh objects for every process. It looks like that works, but I'm not sure if it's the best solution. I'm not sure because in production some times crash saving or showing problems with "merge objects" or some times the error shows "SQL file is corrupted". (╯°□°)╯︵ ┻━┻
Cinquain
4 years ago
That video was fire!
Cinquain
4 years ago
Fire
iosmkarim
3 years ago
Hi, if I have more than one child context I need to follow the same saving mechanism right? Multiple child context should have same Main context?
HELP & SUPPORT