Core Data Background Context vs Main Context

Hi,

I'm working on an app where I'm using Core Data to store some information on persistent storage. I'm having issues understanding and deciding how to deal with read/write operations.

At the moment, I have a QuoteGroupStorageManager class that wraps most of the operations in easy to use functions. You can see it below.

You can see that I'm fetching on the main context, and editing/deleting/inserting (apart from the addQuoteToGroup which I do on the mainContext because I use relationships, and the quoteGroup belongs on the mainContext - otherwise I get an error with illegal... object belongs to a diff context).

My question is, am I doing things right, in terms of performance? I'm using SwiftUI for the UI and when I delete a QuoteGroup for example, the UI is freezing for a while, until I make a new deletion. I'm wondering if it's because of the mixture of backgroundContext and mainContext. Which one should I use?




Thanks!
I would recommend using the viewContext for everything by default.

If you hit a real life scenario where this is causing an issue I would investigate how to solve that issue at that point, but it’s better to avoid complexity until you need it.

(To be clear I think this is a good question and this is not an intuitive answer. I think the contexts could be better labelled as “default” and “expert” or something like that)
Core Data Background Context vs Main Context
 
 
Q