viewContext to fetch data in Xcode 8 Sample code?

I created a new project in Xcode 8 using the Master Detail Application template w/ Core Data, and the sample code uses the viewContext to fetch data. Isn't this wrong? If it isn't, what am I misunderstanding?

Answered by malhal in 147070022

The context is highly efficient and is designed to be used from the main thread. I suppose the main reason it is used so the objects in the context can be accessed for use updating the views. Then also say you make changes to the data from the UI, if that wasn't done on the main thread then you wouldn't be able to notify the user about save or validation errors and your data and UI would go out of sync and to resolve that would require a very complicated architecture.

viewContext is the name for the main context in the brand new persistent container class. Perhaps not the best name, but I reckon the reason it was chosen is since your UI code, i.e. view code, runs on the main thread, so they chose viewContext as the name. Personally I think mainContext would have been a better name reducing a level of indirection in understanding. Actually I'll submit a bug report suggesting that thanks for brining it up.

Submitted bug #26883863 suggesting renaming this property to mainContext to prevent this confusion with views.

Right it implies mainContext which is ran on the main thread, so my question still stands. Why use the main thread to handle core data fetching? The main thread should be used for interacting with the UI and updating the UI only from my understanding.

Accepted Answer

The context is highly efficient and is designed to be used from the main thread. I suppose the main reason it is used so the objects in the context can be accessed for use updating the views. Then also say you make changes to the data from the UI, if that wasn't done on the main thread then you wouldn't be able to notify the user about save or validation errors and your data and UI would go out of sync and to resolve that would require a very complicated architecture.

viewContext to fetch data in Xcode 8 Sample code?
 
 
Q