核心数据错误代码=134030“保存时出错”,没有用户信息

Posted

技术标签:

【中文标题】核心数据错误代码=134030“保存时出错”,没有用户信息【英文标题】:Core Data error code=134030 "An error occurred while saving" with no userInfo 【发布时间】:2016-09-13 21:28:56 【问题描述】:

我有一个使用 Xcode 8 GM 种子在 Swift 3 中构建的应用程序。该应用程序有一个键盘扩展。我将共享组目录用于 sqlite db 位置,并且使用框架完成数据库访问。在我尝试从键盘调用 SaveContext() 之前,一切正常。我收到标题错误代码=134030 中提到的错误。消息显示“保存时发生错误”并且没有其他信息。我没有 userInfo 来解决潜在的错误。

我已经转储了一些调试信息,以确保我正在查看我正在尝试更新的同一项目的同一实例。这是一个非常简单的更新,只是在使用某些东西时增加一个使用计数器。从键盘扩展保存有什么问题吗?它们是否以只读模式打开?

这是 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

在 catch 中,我正在检查错误,但无法从中获取更多信息。将其转换为 NSError 后,它仍然无济于事,因为 userInfo 是一个空字典。 我还没有实现错误处理,但是我可以看到键盘中的项目,所以检索没有问题。只有保存时才会崩溃。 我尝试更新的地方非常简单:

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 吗?

谢谢, 迈克

【问题讨论】:

【参考方案1】:

我想通了。原来是因为键盘的“允许完全访问”选项被关闭了。因此,如果您希望能够写入共享组文件夹,包括更新 sqlite 数据库,您必须安装应用程序和键盘并首先选中“允许完全访问”选项。然后您可以返回 Xcode 并在调试模式下运行扩展。设置被保留,一切正常。

迈克

【讨论】:

以上是关于核心数据错误代码=134030“保存时出错”,没有用户信息的主要内容,如果未能解决你的问题,请参考以下文章

使用 iphone sdk 在钥匙串中保存时出错

使用opencv通过Filestorage保存时出错

spark-redshift - 使用 Spark 2.1.0 保存时出错

正确编译的代码(没有错误)给出分段错误核心转储

核心数据错误 133020:在保存中合并问题:

核心数据 NSManagedObject 没有有效的 NSEntityDescription