【问题标题】:Cannot create an SQL store with a nil URL无法使用 nil URL 创建 SQL 存储
【发布时间】:2016-03-14 21:30:56
【问题描述】:

所以我正在尝试使用父应用存储的数据创建一个今天的小部件扩展。

我正在尝试通过创建Persistent Store Coordinator 来实现这一点,就像您在父应用中所做的那样,并使用主应用数据库的 URL。

但是,这样做时会出现以下错误;

2016-03-13 12:59:02.155 TodayWidget[10942:1456153] 继承失败 来自 5079 的 CoreMedia 权限:(null)2016-03-13 12:59:02.336 TodayWidget[10942:1456112] *** 由于未捕获而终止应用程序 异常 'NSInvalidArgumentException',原因:'无法创建 SQL 使用 nil URL 存储。'

这就是我在 Today 小部件视图控制器中所做的事情。 containerPath 与今天扩展目标的包标识符相同。 containerPath?.URLByAppendingPathComponent sql 文件名与父应用核心数据栈 sql 文件名相同,modelURL 与父应用中相同。

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

有人知道我该如何解决这个错误吗?

PS:我已经启用并创建了应用程序组,并将今天的小部件添加为核心数据模型和子类的目标。

【问题讨论】:

  • 不是已经有人解释过原因了吗,上次你问然后删除了那个问题?
  • @PetahChristian 不,他们只是说我应该调查 containerPath,而且问题不是很详细,问题本身也不清楚。
  • 你为什么不改进原来的问题?关于错误没有任何改变。和以前一样,您的containerPath 仍然为零。
  • containerURLForSecurityApplicationGroupIdentifier 必须返回 nil - 看看这里 - stackoverflow.com/questions/26740404/…

标签: ios swift core-data ios8-today-widget today-extension


【解决方案1】:

"com.company.APPNAME.TodayWidget" 看起来很可疑。它看起来像您今天的小部件的捆绑 ID。 containerURLForSecurityApplicationGroupIdentifier() 的参数应该是您在设置 com.apple.security.application-groups 功能时指定的字符串。

【讨论】:

  • 转到您的应用目标 > 功能 > 应用组,检查字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 2019-09-13
  • 2015-07-24
  • 2016-11-19
  • 2019-05-31
相关资源
最近更新 更多