Maybe this question requires a bit of context.
I've been working on my persistence layer using Core Data and found out that Core Data isn't thread-safe and thus requires NSManagedObjectContext to be confined to each one thread only.
So my approach is to create custom background thread NSManagedObjectContext which executes fetching, saving etc, while also to create main thread NSManagedObjectContext which will be used to get NSManagedObject from fetched NSManagedObjectId and pass it to caller method.
By default, Xcode generates template code related to Core Data using lazy var for all NSManagedObjectContext, NSManagedObjectModel etc.
So my question is whether to
use the lazy var instantiation approach for creating NSManagedObjectContext, provided that lazy var initiates an object for each thread trying to access (not thread-safe?)
or
declare separate variables for NSManagedObjectContext in each thread and make all thread-related methods to reference two different NSManagedObjectContext provided that lazy var is thread-safe(?) and created only once when it is accessed regardless of thread.
Thank you in advance!
edit: Anyone who is struggling with Core Data concurrency issue, this article lays out a very nice design pattern to work with as pointed out by Aaron in the comment below!
 
    