CoreData? Hard to Learn, Fun to Apply
I just stick around between NSManagedObject and NSManagedObjectContext since my program doesn’t work appropriately. The situation is I need to do simple CRUD operation utilize CoreData framework provided by Apple. The documentation is quite useful, rich, and clear but not for me as a new developer.
My first imagination is the CRUD operation will involve simple steps with simple syntax such as to save something in UserDefaults. We need to define a value and a key to make persistence happen. But CoreData is really lead me to a mess code for 3 days of total.
We need to define PersistenceStore (whatever is the name, I don’t want to mention it’s precise name. Hahaha), ManagedObjectContext and procedures to save which involving threads concern and concurrency. Really overwhelmed me so much.
One of the most that bring me to a lot of comments and code is the usage of ManagedObjectContext. For the first. I just learn how to save an object to the disk. Since my SQLite file is updated whenever the saving business logic is executed, I dont care with the faulting. Faulting, according to Apple itself, is designed in order to save memory usage since the object remains in its nil condition which occupies no memory till its fired. Not exactly the description but it is the simplest way to understand the concept.
Faulting, in my case, leads confusion when I need to display the objects which included to an array to update the TableView. Because of this fault, my tableView is remained no list displayed but the rows count is exactly same with the array count.
What I am missing in my code, as it turns out, I don’t use the ManagedObjectContext properly. I inserted to ManagedObject within privateManagedObjectContext and when I read the property, I call it from mainManagedObjectContext. Because the former is deallocated on function return, then my ManagedObject turn to its fault state again.
It worth noting :
Take care of ManagedObjectContext and your code will be fine.
Addendum :
I have more problems then, one of them is thread safe issue. I will learn, I will overcome it. Those errors will bend their knee before me. :D