2016-09-13 45 views
0

我有一個使用Xcode 8 GM種子在Swift 3中構建的應用程序。該應用程序有一個鍵盤擴展。我爲sqlite數據庫位置使用共享組目錄,並且使用框架完成數據庫訪問。直到我嘗試從鍵盤調用SaveContext()時,情況才起作用。我收到標題錯誤代碼= 134030中提到的錯誤。該消息顯示「保存時發生錯誤」,並且沒有其他信息。沒有userInfo讓我知道潛在的錯誤。核心數據錯誤代碼= 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是一個空字典。 我還沒有實現錯誤處理,但我可以看到鍵盤上的項目,所以沒有檢索問題。只有在保存時纔會崩潰。 那裏我試圖更新的地方是可笑的簡單:

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 symbolicate libswiftcore?

謝謝, 邁克

回答

0

我想通了。原來,這是因爲鍵盤的「允許完全訪問」選項被關閉。所以,如果你想能夠寫入共享組文件夾,包括更新sqlite數據庫,你必須安裝應用程序&鍵盤,並首先檢查「允許完全訪問」選項。然後您可以返回到Xcode並在調試模式下運行擴展。該設置被保留,一切都應該工作。

Mike