我有一个使用Xcode 8 GM种子在Swift 3中构建的应用程序。该应用程序具有键盘扩展名。我使用共享组目录作为sqlite db位置,并且使用框架完成数据库访问。一切正常,直到我尝试从键盘调用SaveContext()。我收到标题错误代码= 134030中提到的错误。消息说"保存时发生错误"并且没有其他信息。我没有用户信息来解决潜在的错误。
我已经丢弃了一些调试信息,以确保我查看了我尝试更新的同一项目的同一个实例。这是一个非常简单的更新,只是在使用某些内容时递增使用计数器。从键盘扩展程序保存有问题吗?它们是以只读模式打开吗?
这是CoreDataStack的主要内容:
let coreDataStack = CoreDataStack()
public class CoreDataStack {
public func saveContext()
{
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch let error {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
// MARK: - Private
lazy var managedObjectContext: NSManagedObjectContext = {
return self.persistentContainer.viewContext
}()
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let modelUrl = Bundle(identifier: "com.myapp.AppDataKit")?.url(forResource: "MyApp-SE", withExtension: "momd")
let managedObjectModel = NSManagedObjectModel(contentsOf: modelUrl!)!
let container = NSPersistentContainer(name: "MyApp-SE", managedObjectModel: managedObjectModel)
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: try! FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myapp")!.appendingPathComponent("MyAppSEData.sqlite"))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
} // class CoreDataStack
在捕获中我检查错误,我无法从中获取更多信息。在将它转换为NSError后,它仍然没有帮助,因为userInfo是一个空的dict。 我还没有实现错误处理,但我可以看到键盘中的项目,因此检索没有问题。只有在保存时才会崩溃。 我尝试更新的地方非常简单:
item.useCount = NSNumber(value: item.useCount.intValue + 1)
Logger.add(item.debugDescription)
AppData.SaveContext()
AppData是一个具有静态函数的类,用于访问核心数据
public class AppData {
public static func SaveContext() {
coreDataStack.saveContext()
}
...
}
我查看了设备日志,但没有获得更好的信息。我可以在数据工具包中看到两行,然后从libswiftCore.dylib
中看到两行0 libswiftCore.dylib 0x0000000100ab2848 0x100a80000 + 206920
1 libswiftCore.dylib 0x0000000100ab2848 0x100a80000 + 206920
任何人都知道如何在Xcode8中象征libswiftcore?
谢谢, 麦克
答案 0 :(得分:0)
我明白了。事实证明,这是因为键盘的“允许完全访问”选项已关闭。因此,如果您希望能够写入共享组文件夹,包括更新sqlite数据库,则必须安装应用程序&键盘并首先检查“允许完全访问”选项。然后,您可以返回Xcode并在调试模式下运行扩展。保留设置,一切都应该有效。
麦克