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

后端 未结 17 2168
北恋
北恋 2020-12-12 14:35

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

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

I am try

相关标签:
17条回答
  • 2020-12-12 14:56

    I found the best way to find and open the .sqlite database is use this script:

    lsof -Fn -p $(pgrep YOUR_APP_NAME_HERE) | grep .sqlite$ | head -n1
    

    For whatever reason, the output on my Mac always has an extra n character, so I had to copy and paste it to open command. Feel free to modify the above command if you've figured out the right command.

    For the best SQLite browser, I'd recommend TablePlus.

    Adopted from: https://lukaszlawicki.pl/how-to-quickly-open-sqlite-database-on-ios-simulator/

    0 讨论(0)
  • 2020-12-12 14:59

    An answer for a noobs as I was, I spent quite a lot of time in figuring it out. Steps I followed are :

    1. Open finder and Press Command(Windows) + Shift + G.
    2. Go to the folder add ~/Library/Developer
    3. Search for the DB name you've created as in my case it was my.db in SQLite.
    4. Download and install DB browser for SQLite.
    5. Click open database.
    6. Now drag and drop the DB file from your finder.
    0 讨论(0)
  • 2020-12-12 15:01

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

    persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
    

    (Assuming you are only using one persistent store)

    0 讨论(0)
  • 2020-12-12 15:01

    Sorry for the late answer

    i wasnt able to find it in the iphone simulator folder. Then i found out the folder by printing the doc path in log .

    Code:

    NSLog(@"The Path is %@", 
      [[[NSFileManager defaultManager] URLsForDirectory:
         NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
    

    and the path turns out be like this :

    // /Users/<username>/Library/Developer/CoreSimulator/Devices/<app specific id>/data/Containers/Data/Application/<id>/Documents/coredata.sqlite
    
    0 讨论(0)
  • 2020-12-12 15:03

    Here is my solution(works on 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:

    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:
    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
    

    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.
    0 讨论(0)
  • 2020-12-12 15:03

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

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

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

    -com.apple.CoreData.SQLDebug 1

    0 讨论(0)
提交回复
热议问题