核心数据文件的位置 iOS 10

Core Data file's Location iOS 10

我正在尝试使用 SQLite 浏览器查看我的核心数据对象。我无法找到核心数据保存其 sql 文件的位置。我查看了应用程序文档文件夹,但那里什么也没有。

你知道IOS10(simulator)的核心数据保存在什么地方吗?

试试这个我还没有在 ios 10 上检查过,但它在以前的所有版本中都有效

产品 > 方案 > 编辑方案 > 运行 > 参数

在“启动时传递的参数”中添加此参数

-com.apple.CoreData.SQLDebug 1

每次应用程序启动时它会打印数据库路径 See This argument look like this

I tested it on XCode 8 Swift 3 OS macOS Sierra

IOS 10

在你的 Appdelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
     print(urls[urls.count-1] as URL)

    return true
}

1.常量 .documentDirectory 表示我们正在寻找文档目录

2。常量 .userDomainMask 将我们的搜索限制在我们应用程序的沙箱中。

输出

file:///Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Data/Application/3B373C93-71A2-46E9-8AF7-EF407C39429F/Documents/

单击前往 -> 前往文件夹 -> 粘贴路径 -> 按 Enter

然后转到图书馆 -> 应用程序支持 -> filename.sqlite

已编辑

打开终端并输入 find ~ -name 'HitTest.sqlite' 并回车。

Ashoks-MacBook-Pro:Desktop Ashok$ find ~ -name 'HitTest.sqlite'
/Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Data/Application/3B373C93-71A2-46E9-8AF7-EF407C39429F/Documents/HitTest.sqlite

从上面的输出可以清楚的看到你的sqlite db的路径

可以用DB Browser for SQLite打开

对于XCODE 8.0 (iOS 10):-

.sqlite 持久存储的路径是:

/Users/Ashish/Library/Developer/CoreSimulator/Devices/"YourDeviceId"/data/Containers/Data/Application/"Application Id"/Library/Application Support/"FileNamePassedInPersistentContainer.sqlite"

Swift 3.x

在您的 AppDelegate 文件中找到 didFinishLaunchingWithOptions 并添加此行。

let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
print("\(path)")

由于您的应用程序的核心数据文件的位置是可变的,找到一个后,您可以使用像 SQLiteFlow 这样的 SQLite 浏览器来浏览您的核心数据文件的内容。原因很简单,它可以处理 SQLite 文件位置的变化。来自 SQLiteFlow 的文档:

Handle database file name or directory changes. This makes SQLiteFlow can work friendly with your SQLite database in iOS simulator.

对于swift 5

func getCoreDataDBPath() {
        let path = FileManager
            .default
            .urls(for: .applicationSupportDirectory, in: .userDomainMask)
            .last?
            .absoluteString
            .replacingOccurrences(of: "file://", with: "")
            .removingPercentEncoding

        print("Core Data DB Path :: \(path ?? "Not found")")
    }

这将给出 sqlite 文件 (dbname.sqlite) 的详细路径

print("CoreData path :: \(self.getCoreDataDBPath())")

注意:点击前往 -> 前往文件夹 -> 在此处粘贴路径 -> 输入

设置后:

// available for iOS/iPadOS/Catalyst/macOS, other systems are not tested yet
context.persistentStoreCoordinator?.persistentStores.forEach({
    if let url = [=10=].url {
        print(url)
    }
})

对于 Swift v5.x,这将起作用。

func getCoreDataDBPath() {
        let path = FileManager
            .default
            .urls(for: .applicationSupportDirectory, in: .userDomainMask)
            .last?
            .absoluteString
            .replacingOccurrences(of: "file://", with: "")
            .removingPercentEncoding

        print("Core Data DB Path :: \(path ?? "Not found")")
    }

打印此路径后,您可以使用 DB Browser for SQL Lite 并查看所有表格。