【发布时间】:2017-02-04 23:28:32
【问题描述】:
我正在尝试使用 SQLite 浏览器查看我的核心数据对象。我无法找到核心数据将其 sql 文件保存在哪里。我查看了应用程序文档文件夹,但那里什么也没有。
你知道IOS 10(模拟器)中的核心数据在哪里保存它的SQLite文件吗?
【问题讨论】:
标签: ios swift core-data ios-simulator ios10
我正在尝试使用 SQLite 浏览器查看我的核心数据对象。我无法找到核心数据将其 sql 文件保存在哪里。我查看了应用程序文档文件夹,但那里什么也没有。
你知道IOS 10(模拟器)中的核心数据在哪里保存它的SQLite文件吗?
【问题讨论】:
标签: ios swift core-data ios-simulator ios10
我在 XCode 8 Swift 3 OS macOS Sierra 上对其进行了测试
IOS 10
在您的 Appdelegate.swif 中t
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
然后转到库 -> 应用程序支持 -> 文件名.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打开。
【讨论】:
试试这个,我没有在 ios 10 上检查过,但它在以前的所有版本中都可以工作
产品 > 方案 > 编辑方案 > 运行 > 参数
在“Arguments Passed on launch”中添加此参数
-com.apple.CoreData.SQLDebug 1
每次应用程序启动时,它都会打印到数据库的路径 See This argument look like this
【讨论】:
Swift 3.x
在您的 AppDelegate 文件中找到 didFinishLaunchingWithOptions 并添加这一行。
let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
print("\(path)")
【讨论】:
对于 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())")
注意:点击Go -> Go to Folder -> Paste path here -> Enter
【讨论】:
设置后:
// available for iOS/iPadOS/Catalyst/macOS, other systems are not tested yet
context.persistentStoreCoordinator?.persistentStores.forEach({
if let url = $0.url {
print(url)
}
})
【讨论】:
lazy var persistentContainer: NSPersistentContainer 我的代码看起来有点不同:persistentContainer.persistentStoreCoordinator.persistentStores.forEach({ if let url = $0.url { print(url) } })
由于您的应用程序的核心数据文件的位置是可变的,找到一个后,您可以使用 SQLite 浏览器,如SQLiteFlow 来浏览您的核心数据文件的内容。原因很简单,它可以处理 SQLite 文件位置的变化。来自 SQLiteFlow 的文档:
处理数据库文件名或目录更改。这使得 SQLiteFlow 可以在 iOS 模拟器中与您的 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 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 查看所有表。
【讨论】: