ios 如何检查存储在我的核心数据数据库中的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10239634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:57:56  来源:igfitidea点击:

How can I check what is stored in my Core Data Database?

ioscore-data

提问by jwknz

I am making an app that relies on Core Data. I am able to enter data into a text field and store it.

我正在制作一个依赖于 Core Data 的应用程序。我能够在文本字段中输入数据并存储它。

But I need to know if the data is being stored.

但我需要知道数据是否正在存储。

I am trying to make a detailView to my tableView and I am not getting any results. Now I am wondering is that because I am doing something wrong with my code, or is the data nto being stored properly.

我正在尝试为我的 tableView 创建一个 detailView,但没有得到任何结果。现在我想知道是因为我的代码做错了,还是数据没有正确存储。

How can I see what is stored in the app's CoreData database?

如何查看应用程序的 CoreData 数据库中存储的内容?

采纳答案by Chris Chen

If you use sqlite as the storage media for Core Data, you can run your app in simulator and try to check the database file which is located in the sandbox's Library folder.

如果您使用 sqlite 作为 Core Data 的存储介质,您可以在模拟器中运行您的应用程序并尝试检查位于沙箱的 Library 文件夹中的数据库文件。

The path shall be something like: ~/Library/Application Support/iPhone Simulator/5.1/Applications/3BF8A4B3-4959-4D8F-AC12-DB8EF4C3B6E1/Library/YourAppName.sqlite

路径应类似于:~/Library/Application Support/iPhone Simulator/5.1/Applications/3BF8A4B3-4959-4D8F-AC12-DB8EF4C3B6E1/Library/YourAppName.sqlite

To open the sqlite file, you need a tool. I use a free tool called Liya (http://itunes.apple.com/us/app/liya/id455484422?mt=12).

要打开 sqlite 文件,您需要一个工具。我使用名为 Liya 的免费工具(http://itunes.apple.com/us/app/liya/id455484422?mt=12)。

回答by Zaster

Here is my solution(works on iOS 9):

这是我的解决方案(适用于 iOS 9):

I use an automator/bash script that open the database in sqllitebrowser. the script finds the latest installed app in the simulator. Instructions:

我使用自动程序/bash 脚本在 sqllitebrowser 中打开数据库。该脚本在模拟器中查找最新安装的应用程序。指示:

  1. Install DB Browser for SQLite (http://sqlitebrowser.org/)
  2. Create new workflow in Apple Automator.
  3. Drag "Run Shell script block" and paste this code:
  1. 为 SQLite 安装数据库浏览器 ( http://sqlitebrowser.org/)
  2. 在 Apple Automator 中创建新的工作流程。
  3. 拖动“运行 Shell 脚本块”并粘贴以下代码:
cd ~/Library/Developer/CoreSimulator/Devices/
cd `ls -t | head -n 1`/data/Containers/Data/Application 
cd `ls -t | head -n 1`/Documents
open -a DB\ Browser\ for\ SQLite ./YOUR_DATABASE_NAME.sqlite
cd ~/Library/Developer/CoreSimulator/Devices/
cd `ls -t | head -n 1`/data/Containers/Data/Application 
cd `ls -t | head -n 1`/Documents
open -a DB\ Browser\ for\ SQLite ./YOUR_DATABASE_NAME.sqlite

enter image description here

在此处输入图片说明

  1. (Optional) Convert this workflow to application, save it and drag it to your dock. To refresh the database just click on the app icon.
  1. (可选)将此工作流转换为应用程序,保存并拖动到 Dock。要刷新数据库,只需单击应用程序图标。

回答by Vahid

Swift 4, 5

斯威夫特 4、5

Add this line in AppDelegate>> didFinishLaunchingWithOptionsfunction:

AppDelegate>>didFinishLaunchingWithOptions函数中添加这一行:

print("Documents Directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found!")

Your modelName.sqlitefile will be there.

您的modelName.sqlite文件将在那里。

You can open it with any SQLite browser tools like http://sqlitebrowser.org/that is free.

您可以使用任何免费的 SQLite 浏览器工具(例如http://sqlitebrowser.org/)打开它。

回答by Ivan Iliev

The folder where the db is stored has recently been changed and is not where you'd expect to find it. Here's what I used to solve this: First add this code to your viewDidLoad or applicationDidFinishLaunching:

存储 db 的文件夹最近已更改,并且不是您希望找到的文件夹。这是我用来解决此问题的方法:首先将此代码添加到您的 viewDidLoad 或 applicationDidFinishLaunching:

    #if TARGET_IPHONE_SIMULATOR
    // where are you?
    NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
    #endif

Got this from here: where is the documents directory for the ios 8 simulator

从这里得到这个: ios 8 模拟器的文档目录在哪里

This will reveal the actual location of your app in the console during runtime. Then use the SQLiteBrowserto check the contents of your SQLite DB.

这将在运行时在控制台中显示您的应用程序的实际位置。然后使用SQLiteBrowser检查 SQLite DB 的内容。

Worked like a charm for me ;)

对我来说就像一个魅力;)

回答by Jody Hagins

As said before, you can use the sqllite command line tool.

如前所述,您可以使用 sqllite 命令行工具。

However, you can also set the debug flag, which will dump out all sql commands as they execute through core data.

但是,您也可以设置调试标志,这将在通过核心数据执行时转储所有 sql 命令。

Edit the scheme, and add this in the "Arguments Passed on Launch"

编辑方案,并将其添加到“启动时传递的参数”中

-com.apple.CoreData.SQLDebug 1

-com.apple.CoreData.SQLDebug 1

回答by ganzogo

In Swift 3, you have the the NSPersistentContainerand you can retrieve the path from there like this:

在 Swift 3 中,您NSPersistentContainer可以从那里检索路径,如下所示:

persistentContainer.persistentStoreCoordinator.persistentStores.first?.url

(Assuming you are only using one persistent store)

(假设您只使用一个持久存储)

回答by Rocky Balboa

As Far as macOS Sierra version 10.12.2 and XCode 8 is concerned

就 macOS Sierra 版本 10.12.2 和 XCode 8 而言

The folder should be here:

文件夹应该在这里:

/Users/$username$/Library/Developer/CoreSimulator/Devices/$DeviceID$/data/Containers/Data/Application/$ApplicationID$/Library/Application Support/xyz.sqlite

/Users/$username$/Library/Developer/CoreSimulator/Devices/$DeviceID$/data/Containers/Data/Application/$ApplicationID$/Library/Application Support/xyz.sqlite

To get the device id - Open terminal and paste:

要获取设备 ID - 打开终端并粘贴:

instruments -s devices

instruments -s devices

回答by staticVoidMan

The other solutions are either old or does not direct you easily or quickly to the SQLite files, so I came up with my own solution using FileManager.SearchPathDirectory.applicationSupportDirectorythat gets the exact path where the sqlite file will be.

其他解决方案要么过时,要么无法轻松或快速地将您定向到 SQLite 文件,因此我想出了自己的解决方案,使用FileManager.SearchPathDirectory.applicationSupportDirectory它获取 sqlite 文件所在的确切路径。

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

    print(path ?? "Not found")
}

whereIsMySQLite()will print the path which you can simply copy-paste on your Mac here:

whereIsMySQLite()将打印您可以简单地在 Mac 上复制粘贴的路径:

Finder > Go > Go to folder

Finder > 前往 > 前往文件夹

回答by Nik Kov

1) Get path of sql database of your simulator.

1)获取模拟器的sql数据库路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@", [paths objectAtIndex:0]);

2) Open Finder. Press Cmd+Shift+G. Paste something, that you got from paragraph 1. Ex:

2) 打开查找器。按Cmd+ Shift+ G。粘贴一些你从第 1 段得到的东西。例如:

/Users/USERNAME/Library/Developer/CoreSimulator/Devices/701BAC5F-2A42-49BA-B733-C39D563208D4/data/Containers/Data/Application/DCA9E9C7-5FEF-41EA-9255-6AE9A964053C/Documents

3) Download in AppStore programm like SQLPro.

3) 在 AppStore 中下载类似SQLPro 的程序

4) Open file in folder with name "ProjectName.sqlite".

4) 打开文件夹中名为“ProjectName.sqlite”的文件。

GOOD JOB! You will see something like this: enter image description here

做得好!你会看到这样的事情: 在此处输入图片说明

回答by clearwater82

Download the SQLite Browser from here.

这里下载 SQLite 浏览器。

Run your app in the Simulator. The app should be copied to a path on your Mac that looks like:

在模拟器中运行您的应用程序。该应用程序应复制到 Mac 上如下所示的路径:

/Users/$your username$/Library/Application Support/iPhone Simulator/$your iphone simulator version$/Applications/

Once you locate your app, you have to dig deeper to find the sqlite db (It's usually under Documents).

一旦你找到你的应用程序,你必须深入挖掘才能找到 sqlite db(它通常在 Documents 下)。