A Code Sample to use Core Data Stack as a Singleton

I used to create an instance of my Core Data Stack in AppDelegate file and accessed it from other View Controllers. But I have to modify it because iOS 11 forces to access AppDelegate file in the main thread. So I made my Core Data Stack as a singleton.

If you are looking for a code sample to make your core data stack as a singleton. Please refer the code below.

class func sharedInstance() -> CoreDataStack {
    struct Static {
        static let instance = CoreDataStack()
    }
    return Static.instance
}

Leave a Reply