iosswiftcore-dataios8-today-widgettoday-extension

Cannot create an SQL store with a nil URL


So I am trying to create a today widget extension using the data stored by the parent app.

I am trying to do so by creating a Persistent Store Coordinator as you do in parent app and using the URL for the main app database.

However when doing so I get the following error;

2016-03-13 12:59:02.155 TodayWidget[10942:1456153] Failed to inherit CoreMedia permissions from 5079: (null) 2016-03-13 12:59:02.336 TodayWidget[10942:1456112] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an SQL store with a nil URL.'

Here's what I am doing in the Today widget view controller. The containerPath is the same as the bundle identifier for the today extension target. The containerPath?.URLByAppendingPathComponent sql file name is the same as the parent app core data stack sql file name as well as the the modelURL is the same as it is in the parent app.

var context: NSManagedObjectContext!

let fileManager = NSFileManager.defaultManager()
var containerPath = fileManager.containerURLForSecurityApplicationGroupIdentifier("com.company.APPNAME.TodayWidget")

containerPath = containerPath?.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
let modelURL = NSBundle.mainBundle().URLForResource("APPNAME", withExtension: "mom")
let model = NSManagedObjectModel(contentsOfURL: modelURL!)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model!)
    do {
        try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: containerPath, options: nil)
    } catch {
     print("yellow")
    }

  context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
  context.persistentStoreCoordinator = coordinator

Does anybody know how I can resolve the error ?

PS: I have enabled and created app groups and added the today widget as a target for the core data model and the subclasses.


Solution

  • "com.company.APPNAME.TodayWidget" looks suspicious. It looks like the bundle ID for your today widget. The argument to containerURLForSecurityApplicationGroupIdentifier() should be the string you specified when you set up the com.apple.security.application-groups capability.