UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is Core Data always using an SQLite DB behind the scenes? - Project 11

Forums > SwiftUI

Hi all,

Is Core Data always using an SQLite DB behind the scenes?

Should I be able to find a .sqlite file somewhere that stores the books? Or is persisted in another way?

I am working on project 11 and was curious about this.

I see

    // MARK: - Core Data Saving support

    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

take care Bill

4      

Yes, there is always a SQLite behind it. You can find it in the documents folder. As with every new build a new documents folder is created on the simulator it's getting quite cumbersome to search it manually everytime.

https://www.codefellows.org/blog/core-data-and-database-browser-for-sqlite/

Create a function in your app and print it out. F.e. on app start.

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

Or you could use the neat little app: https://apps.apple.com/us/app/core-data-lab/id1460684638?mt=12

4      

To be precise, it doesn't have to be a SQLite database, there's other possibilities: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/PersistentStoreFeatures.html

Without any numbers to base this on, I'd say most Core Data setups are backed by SQLite.

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.