Most developers don't worry where Core Data stores its data and many probably don't know where the persistent store is located in the application's sandbox. To debug Core Data issues, however, it can be useful to access the persistent store and inspect its data. In this post, I show you where you might find the data Core Data stores.
In Memory
You probably know that Core Data supports several types of persistent stores, including an in-memory persistent store. The data of an in-memory persistent store is never written to disk, which implies that the data doesn't persist across application launches. In that scenario, you won't find the persistent store in the application's sandbox.
NSPersistentContainer
Let's start with a blank Xcode project. Open Xcode and create a new project by choosing the Single View App template form the iOS > Application section.
Name the project Notes and check Use Core Data at the bottom.
Open AppDelegate.swift. Because we checked the Use Core Data checkbox, Xcode added a property to the AppDelegate
class. The property is named persistentContainer
and it is of type NSPersistentContainer
. I don't recommend setting up the Core Data stack in the application delegate. I explain why in Core Data Fundamentals.
If you use the NSPersistentContainer
class, then it is easy to find the location of the persistent store. The NSPersistentContainer
class defines a class method, defaultDirectoryURL()
, that returns the location of the directory in which the persistent store is stored. The value the defaultDirectoryURL()
method returns is of type URL
and differs between Apple's platforms. On iOS, for example, the defaultDirectoryURL()
method returns a URL
object pointing to the Library > Application Support directory in the application's sandbox.
Choose Window > Devices and Simulators from Xcode's menu. Select your device on the right and choose the application from the list of Installed Apps at the bottom. Click the gear icon and choose Download Container....
Right-click the downloaded container and choose Show Package Contents form the contextual menu. The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension .sqlite.
It is possible that you don't see the persistent store in the Application Support directory. Why is that? The persistentContainer
property of the AppDelegate
class is a lazy property, which means that the NSPersistentContainer
instance is only created when it is accessed for the first time. This is easy to solve by adding one line to the application(_:didFinishLaunchingWithOptions:)
method.
import UIKit
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: - Application Life Cycle
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Access Persistent Container
let _ = persistentContainer
return true
}
...
}
It's important that you don't refer to it as a Core Data database because, as you know, Core Data isn't a database and you shouldn't treat or use it as such. The framework supports several types of persistent stores, including SQLite and XML.
Persistent Store Coordinator
If you don't use the NSPersistentContainer
class, and I recommend that you don't, then you decide where Core Data stores its data. As part of the Core Data stack setup, one or more persistent stores are added to the persistent store coordinator. To add a persistent store to the persistent store coordinator, you invoke the addPersistentStore(ofType:configurationName:at:options:)
method. The third argument is of type URL?
. The URL
object specifies the location of the persistent store.
But notice that the third argument is of an optional type. What happens if you pass nil
instead of a URL
object? I'm afraid the API is a bit confusing and Apple's documentation isn't clear about what to expect. Remember that Core Data supports an in-memory persistent store. Because the contents of an in-memory persistent store are never written to disk, there's no need to pass a URL
object to the addPersistentStore(ofType:configurationName:at:options:)
method. If you opt for a SQLite or XML persistent store and don't pass a valid URL
object to addPersistentStore(ofType:configurationName:at:options:)
, then an exception is thrown at runtime. That's another mystery solved.
Core Data and the Simulator
You sometimes need to access or inspect the persistent store during development. This is straightforward if you use a physical device. I outlined the steps earlier in this post. When you use the simulator, this is less obvious. Where can you find the Core Data persistent store in the simulator? The location in the application's sandbox is identical, but where can you find the application's sandbox on your development machine?
The location depends on the Xcode version you are using. I recommend installing SimPholders or a similar application. It adds a convenient menu bar item that provides quick access to the application's sandbox.
Keep This in Mind
Before I leave you, I want to emphasize that you should never ask "Where is the Core Data database located?" There is no such thing as a "Core Data database". Remember that Core Data isn't a database, it is a persistence framework that can optionally write data to disk. Do you want to learn more? Check out Core Data Fundamentals.