20

I am trying to us SQLite Browsers to see my Core Data objects. I am not able to find where does the core data save its sql file. I looked into the app documents folder but there is nothing there.

Do you know where does the core data in IOS 10(simulator) save its SQLite files on?

Idan Aviv
  • 1,253
  • 2
  • 13
  • 23

8 Answers8

38

I tested it on XCode 8 Swift 3 OS macOS Sierra

IOS 10

In Your 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. The constant .documentDirectory says we are looking for Document directory

2. The constant .userDomainMask to restrict our search to our application's sandbox.

output

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

Click on Go -> Go to Folder -> Paste Path -> Hit Enter

enter image description here

Then Go to Library -> Application Support -> filename.sqlite

enter image description here

Edited

OR

open your terminal and type find ~ -name 'HitTest.sqlite' and hit enter.

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

From above output you can clearly see the path of your sqlite db

You can use DB Browser for SQLite to open.

Ashok R
  • 19,892
  • 8
  • 68
  • 68
16

Just try this i haven't check it on ios 10 but its working in previous all versions

Product > Scheme > Edit Scheme > Run > Arguments

Add this argument in "Arguments Passed on launch"

-com.apple.CoreData.SQLDebug 1

Everytime when application launches It will print path to database See This argument look like this

Jitendra Modi
  • 2,344
  • 12
  • 34
  • Thank you for the respond. I have try this but it don't work. I put under arguments passed on launch, -com.apple.CoreData.SQLDebug 1, but this don't print anything. Do you know why ? – Idan Aviv Sep 27 '16 at 11:47
  • This is a better option instead of finding the file path because if your application has shared Db (with extension or other group app) then it would not be in the same location as file path but at shared location. So it is good to use this way to find the location of the sqlite db. – Sundeep Aug 13 '17 at 16:08
  • This setting works on iOS 11 and Xcode 9. The DB file is still at /Library/Application Support/ directory. – Julia Zhao Nov 16 '17 at 18:20
8

Swift 3.x

Locate didFinishLaunchingWithOptions in your AppDelegate file and add this line.

let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
print("\(path)")
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Chetan
  • 881
  • 14
  • 22
5

For 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")")
    }

This will give detailed path till sqlite file (dbname.sqlite)

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

Note: Click on Go -> Go to Folder -> Paste path here -> Enter

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
2

After setup:

// available for iOS/iPadOS/Catalyst/macOS, other systems are not tested yet
context.persistentStoreCoordinator?.persistentStores.forEach({
    if let url = $0.url {
        print(url)
    }
})
hstdt
  • 5,652
  • 2
  • 34
  • 34
  • Having a `lazy var persistentContainer: NSPersistentContainer` my code looks a little different: `persistentContainer.persistentStoreCoordinator.persistentStores.forEach({ if let url = $0.url { print(url) } }) ` – dequin Aug 26 '20 at 22:25
1

For XCODE 8.0 (iOS 10):-

The path to the .sqlite persistent store is:

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

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Nupur Sharma
  • 1,106
  • 13
  • 15
0

Since your app's Core Data file's location is changeable, after you find one, you can use a SQLite Browser like SQLiteFlow to browse your Core Data file's content. The reason is straightforward, it can handle SQLite file location changes. From SQLiteFlow's document:

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

Tim
  • 66
  • 2
0

For Swift v5.x, this will work.

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")")
    }

Once this path is printed, you can use DB Browser for SQL Lite and see all the tables.

Md. Yamin Mollah
  • 1,609
  • 13
  • 26