title: How to use Core data in widget today extension?
body: <p>I'd like to use core data in today extension. 
  I tried some ways down below. </p>
  
  <ol>
  <li><p>create app group and target to both app and today extension!</p></li>
  <li><p>create CoreDataStack class following <a href="https://swifting.io/blog/2016/09/25/25-core-data-in-ios10-nspersistentcontainer/" rel="nofollow noreferrer">this link</a></p></li>
  </ol>
  
  <p>full code is here :</p>
  
  <pre><code>final class CoreDataStack {
  static let sharedStack = CoreDataStack()
  var errorHandler: (Error) -&gt; Void = {_ in }
  //#1
  lazy var persistentContainer: NSPersistentContainer = {
      let container = NSPersistentContainer(name: "spark")
      container.loadPersistentStores(completionHandler: { [weak self](storeDescription, error) in
          if let error = error {
              NSLog("CoreData error \(error), \(error._userInfo)")
              self?.errorHandler(error)
          }
      })
      return container
  }()
  
  //#2
  lazy var viewContext: NSManagedObjectContext = {
      return self.persistentContainer.viewContext
  }()
  
  //#3
  // Optional
  lazy var backgroundContext: NSManagedObjectContext = {
      return self.persistentContainer.newBackgroundContext()
  }()
  
  //#4
  func performForegroundTask(_ block: @escaping (NSManagedObjectContext) -&gt; Void) {
      self.viewContext.perform {
          block(self.viewContext)
      }
  }
  
  //#5
  func performBackgroundTask(_ block: @escaping (NSManagedObjectContext) -&gt; Void) {
      self.persistentContainer.performBackgroundTask(block)
  }
  
  
  
  private init() {
      //#1
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(mainContextChanged(notification:)),
                                             name: .NSManagedObjectContextDidSave,
                                             object: self.managedObjectContext)
  
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(bgContextChanged(notification:)),
                                             name: .NSManagedObjectContextDidSave,
                                             object: self.backgroundManagedObjectContext)
  }
  
  deinit {
      NotificationCenter.default.removeObserver(self)
  }
  
  //#2
  lazy var libraryDirectory: NSURL = {
      let urls = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)
      return urls[urls.count-1] as NSURL
  }()
  
  //#3
  lazy var managedObjectModel: NSManagedObjectModel = {
      let modelURL = Bundle.main.url(forResource: "spark", withExtension: "momd")!
      return NSManagedObjectModel(contentsOf: modelURL)!
  }()
  
  //#4
  lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
      let coordinator = NSPersistentStoreCoordinator(managedObjectModel:
          self.managedObjectModel)
      let url = self.libraryDirectory.appendingPathComponent("spark.sqlite")
      do {
          try coordinator.addPersistentStore(ofType:
              NSSQLiteStoreType,
                                             configurationName: nil,
                                             at: url,
                                             options: [
                                              NSMigratePersistentStoresAutomaticallyOption: true,
                                              NSInferMappingModelAutomaticallyOption: true
              ]
          )
      } catch {
          // Report any error we got.
          NSLog("CoreData error \(error), \(error._userInfo)")
          self.errorHandler(error)
      }
      return coordinator
  }()
  
  //#5
  lazy var backgroundManagedObjectContext: NSManagedObjectContext = {
      let coordinator = self.persistentStoreCoordinator
      var privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
      privateManagedObjectContext.persistentStoreCoordinator = coordinator
      return privateManagedObjectContext
  }()
  
  //#6
  lazy var managedObjectContext: NSManagedObjectContext = {
      let coordinator = self.persistentStoreCoordinator
      var mainManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
      mainManagedObjectContext.persistentStoreCoordinator = coordinator
      return mainManagedObjectContext
  }()
  
  //#7
  @objc func mainContextChanged(notification: NSNotification) {
      backgroundManagedObjectContext.perform { [unowned self] in
          self.backgroundManagedObjectContext.mergeChanges(fromContextDidSave: notification as Notification)
      }
  }
  @objc func bgContextChanged(notification: NSNotification) {
      managedObjectContext.perform{ [unowned self] in
          self.managedObjectContext.mergeChanges(fromContextDidSave: notification as Notification)
      }
  }
  }
  struct CoreDataServiceConsts {
  static let applicationGroupIdentifier = "group.zz.zz.zz"//example
  }
  
  final class PersistentContainer: NSPersistentContainer {
      internal override class func defaultDirectoryURL() -&gt; URL {
          var url = super.defaultDirectoryURL()
          if let newURL =
              FileManager.default.containerURL(
                  forSecurityApplicationGroupIdentifier: CoreDataServiceConsts.applicationGroupIdentifier) {
              url = newURL
          }
          return url
      }
  </code></pre>
  
  <p>I can get use core data in today extension! but, entity is empty. 
  And I tested code every thins is okay. there is no error (because I save some data for test, I perfectly work.)
  I really don't know about this problem. 
  Is it problem about xcode?</p>