3

我们正在开发 iMessage 扩展程序。它成功地使用了 Core Data。我们需要评估 store.sqlite 文件,但找不到。

我们尝试像这样找到它:

  • 在 Xcode 中:窗口 -> 设备
  • Installed Apps中,选择我们的扩展
  • Download Container ...

但是容器是空的:


更新:

感谢@Mundi 的回答,我们发现了如何获取模型 URL:

file:///var/mobile/Containers/Data/PluginKitPlugin/9C15B67C-8917-4A24-9FB0-BD119C43B3C4/Library/Application%20Support/Model.sqlite

现在我们正在尝试将模型复制到Documents文件夹中,以便以后能够通过 Xcode 将其下载到我们的 MacBook(见上文)。

不幸的是,文件的路径:

NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

又在/var/mobile/Containers/

file:///var/mobile/Containers/Data/PluginKitPlugin/D0BBD375-A8B7-43DD-8486-1909965CAEB0/Documents

我们如何将 Model.sqlite 文件从共享容器下载到我们的 MacBook?

4

2 回答 2

2

实际的 sqlite 文件可能位于共享容器中。

对我有用的是记录商店 URL 并使用它来定位它:

print(container.persistentStoreCoordinator.persistentStores.first!.url!)

产生类似的东西

file:///Users/developer/Library/Developer/CoreSimulator/Devices/2EAE0CD4-7899-45A3-8E83-E7D79DEEA08F/data/Containers/Data/Application/37F48A5E-7DAB-4E30-A752-F5B62826A15A/Library/Application%20Support/Events.sqlite

于 2016-11-07T17:38:26.947 回答
1

您最好的选择是使用邮件共享操作编写导出器。在我的一个应用程序中,我允许用户通过电子邮件发送 sqlite 文件的副本来导出所有数据。

func exportAllDataSqlite() {
    let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

    var newFilePath: URL!
    var mutablePathComponents = [String]()
    var sqliteFileCopied = false

    do {
        let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions())
        for f in directoryContents {
            let pathComponents = f.pathComponents
            if pathComponents.last == "XXXXWhatever your file is called when created by the persisten store.sqliteXXXXX" {
                //create a copy of the file with a dated file name
                mutablePathComponents = pathComponents

                let dateComponents = (Calendar.current as NSCalendar).components([.day, .month, .year], from: Date())
                let dateString = "\(dateComponents.day)-\(dateComponents.month)-\(dateComponents.year)"

                mutablePathComponents[mutablePathComponents.count-1] = "Events App \(dateString).sqlite"


                newFilePath = NSURL.fileURL(withPathComponents: mutablePathComponents)
                do {
                    try FileManager.default.copyItem(at: f, to: newFilePath!)
                    sqliteFileCopied = true
                    print("Copied sqlite file")
                } catch let error as NSError {
                    print(error.localizedDescription)
                }

            }
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }

    if sqliteFileCopied == true {
        //sharing
        let activityItem:URL = newFilePath!

        let objectsToShare = [activityItem]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.completionWithItemsHandler = { activity, success, items, error in
            do {
                try FileManager.default.removeItem(at: newFilePath!)
                print("Deleted file: \(newFilePath!)")
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }

        let excludeActivities = [UIActivityType.airDrop,
                                 UIActivityType.print,
                                 UIActivityType.assignToContact,
                                 UIActivityType.saveToCameraRoll,
                                 UIActivityType.addToReadingList,
                                 UIActivityType.postToFlickr,
                                 UIActivityType.postToVimeo]
        activityVC.excludedActivityTypes = excludeActivities

        self.present(activityVC, animated: true, completion: nil)
    } else {
        print("file not copied so can't be shared")
    }
}

这是我最早的一些 Swift,所以不是很好,但它确实有效。前几天我在我的应用程序的部署副本中使用它来调试问题,只需通过电子邮件发送给自己并在你的 Mac 上使用 Sqlite 查看器打开。

于 2016-12-03T19:14:47.633 回答