Apologies
You must be signed in to watch this lesson.
Core Data Singleton Context
Intermediate Training Core Data
Core Data can be a strange beast with all of the moving parts involved setting up our objects. One particular class called NSManagedObjectContext is especially difficult to predict if you have multiple instances of it interacting with Core Data. In today's lesson, we look at what happens when we lose reference to our context and attempt to show data inside of our UI. The solution to maintain our context across our application is to use a Singleton object that lives on forever inside of our application. Inside of this singleton, we keep our NSPersistentContainer property and derive our context using it.

Comments (15)
edward
6 years ago
Hi Brain, when you reload the tableView after fetching the Company objects from Core Data, don't you need to it on the main thread? DispatchQueue.main.async { self.tableView.reloadData() }
Brian Voong
6 years ago
Denness
6 years ago
Hi Brian, I notice that when a project is created with the core data option enabled, you get the various methods and variables in app delegate. Is the persistent container in app delegate called as some type of singleton? Will it avoid the UI/CoreData bug you demonstrated if you call the context with the code below? let appDelegate = UIApplication.shared.delegate as! AppDelegate let managedContext = appDelegate.persistentContainer.viewContext Are there any pros and cons to this method of using core data?
Brian Voong
6 years ago
Denness
6 years ago
Thank you, definitely learning a lot from this series and I have given core data a try before too so already knew some of the basics.
diehard98
6 years ago
Hi, Brian. Great lesson and tutorial. I just wondered about using Core data with Firebase database. I guess I can use Core data as local copy of cloud database (like Firebase) to avoid fetching all data every time. By having some kind of version number on cloud database, I can compare that version number with local copy version. If cloud database has newer version, I can update local Core data. Is this approach that companies are using to reduce amount of data transferring? Thanks,
Sjeijk
6 years ago
I also have this question
egarciabalda
6 years ago
Awesome!! Thanks for your terrific effort to explain to all of us this subject. I’ll keep trying to understand everything (hard topic for rookies) regardless you provided me with the basics and the key points to face singleton... I can’t believe my own app (neither you if you have a minor chance to look at it), by the moment, still working. Thanks again!!!!
Share App
6 years ago
After you set-up the singleton and run to breakpoint on line 80 at 12 min 2 seconds, why doesn't "context" appear at that breakpoint while only "self" and "company" are showing?
nerddd
6 years ago
Hey Brian, Thank you for so clear tutorials. I've got a question about the usage of Singletons. As I know usage of Singletons is a pretty bad practise cuz as you said it stays in the memory till the app is removed from the memory. Also Singletons are globally accessible which makes it hard for developers to keep track of all those set singletons in the project. So my question is: Do you think Singletons are the best option to use in this example or Dependency injection is a better approach?
Brian Voong
6 years ago
nerddd
6 years ago
Thank you for your answer! I am just trying to clarify for myself when it's still no that bad to use Singletons.
Gabriel
6 years ago
Hi Assuming you need to interrogate the same entity - would you use a singleton to cut down code on multiple queries/predicates within one view controller and if so how? Thank you in anticipation.
Brian Voong
6 years ago
Gabriel
6 years ago
Hey Brian very good, I certainly stumbled into that one. OK let me try again, if I was running multiple queries in one class (not two or more) would I have to establish a connection to Core data and the entity in which the data was stored - every-time I wrote the predicate or can I establish a single connection that works for all queries/predcates, thus cutting the code down considerably
Brian Voong
6 years ago
aldo3
5 years ago
Hi Brian, for additional refactoring, do you recommend that the persistentContainer property be an option? Then guard for it? Thanks
jdhindsa
5 years ago
You are one amazing teacher! I was totally lost with Core Data and now it's making sense! Thank you Brian!
jdhindsa
5 years ago
Can you define a property called "context" within the CoreDataManager class as well so that you don't have to create the context variable locally within the save and fetch functions? I tried doing that, but was getting an error regarding the persistent container. Please advise?
Robert Desaeger
4 years ago
Cudos, for the way you have explained, "Singleton". This is truly the best explanation I have ever had of it. First, I am not a big fan of a Singleton. For many developers use it as the Swiss Army Knife to fix all problems that they can not understand or to create beautiful Spaghetti Code. For I have used Singletons in the past but I have made it a habit to not over do it. I will say it again, you are a very good instructor and I would not hesitate to purchasing other courses from you. Thank you again for putting this together.
Brian Voong
4 years ago
hcri1950
4 years ago
You are the UnSong Here in this. I am looking forward to when you will tackle SwiftUI with JSON ;-) Mr. Voong, can you tell me how much time it takes you to just edit your videos? I am talking about taking a Raw Video and start looking at it and splicing the video so that you are ready to putting the video together? There is a reason why I am asking for I may have something for you that you will be interested in. I promise that you will not regret for replying to my question
Brian Voong
4 years ago
Cinquain
4 years ago
More fire
Abdullah Amer II
4 years ago
Hello, I have one Issue, I followed the tutorial step by step but the companies still didn't show up in the list view ,but when I re-build the app all the entry date show down below but not in the app...where is my mistake ?
Brian Voong
4 years ago
iosmkarim
4 years ago
Best explanation of singleton ever. Brian thanks for such explanation, before seeing that video I wasn't clear enough why we are using singleton in core data. This kind of technical explanation is highly appreciable.
Brian Voong
4 years ago
HELP & SUPPORT