Sqlite File Location Core Data

Core Data file's Location iOS 10

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

Sample Image

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

Sample Image

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.

See or retrieve a Core Data .sqlite file from a device

In Xcode:

  1. Open the Devices window (shift-cmd-2)
  2. Select your device from the list on the left
  3. Select your app from the "Installed Apps" list
  4. Click the gear icon at the bottom of the "Installed Apps" list
  5. Select "Download container"
  6. Save the container somewhere
  7. Xcode will open the container location in under. Ctrl-click on the container and select "Show package contents"
  8. You can now open the documents folder and see your app's files.

unable to find the core data sqlite table location

You can set this environmental variable in your projecct

Xcode > your project > Product menu > Scheme > Edit Scheme > Run

Set this

-com.apple.CoreData.SQLDebug 1
in Arguments passed on Launch.

Everytime your projects runs It will print your path to SQLite database.

Hope this thing help you.

How to pre-load Core Data with a SQLite file that have references to images that were saved using external storage?

Step 1: Create "MyAppSeedData" dir and paste MyApp.sqlite, the MyApp_SUPPORT, the MyApp.sqilte-smh, MyApp.sqilte-wal files inside.

Step 2: Drag MyAppSeedData to the bundle under AppDelegate and tick the box add target.

Step 3: These functions must be in AppDelegate file:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
//If first launch condition == true {
seedData()
//}
return true
}


func seedData() {
let fm = FileManager.default

//Destination URL: Application Folder
let libURL = fm.urls(for: .libraryDirectory, in: .userDomainMask).first!
let destFolder = libURL.appendingPathComponent("Application Support").path
//Or
//let l1 = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last!
//

//Starting URL: MyAppSeedData dir
let folderPath = Bundle.main.resourceURL!.appendingPathComponent("MyAppSeedData").path

let fileManager = FileManager.default
let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if let applicationSupportURL = urls.last {
do{
try fileManager.createDirectory(at: applicationSupportURL, withIntermediateDirectories: true, attributes: nil)
}
catch{
print(error)
}
}
copyFiles(pathFromBundle: folderPath, pathDestDocs: destFolder)
}


func copyFiles(pathFromBundle : String, pathDestDocs: String) {
let fm = FileManager.default
do {
let filelist = try fm.contentsOfDirectory(atPath: pathFromBundle)
let fileDestList = try fm.contentsOfDirectory(atPath: pathDestDocs)

for filename in fileDestList {
try FileManager.default.removeItem(atPath: "\(pathDestDocs)/\(filename)")
}

for filename in filelist {
try? fm.copyItem(atPath: "\(pathFromBundle)/\(filename)", toPath: "\(pathDestDocs)/\(filename)")
}
} catch {
print("Error info: \(error)")
}
}




// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
let modelName = "MyApp"

var container: NSPersistentContainer!

container = NSPersistentContainer(name: modelName)

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()


Related Topics



Leave a reply



Submit