Apple’s Photos.framework, which powers the iOS Photos app and gives developers access to the device’s photo library, uses Core Data under the hood.
You can confirm this in at least two places:
-
Write an app that accesses the photo library and launch it with these command line arguments:
-com.apple.CoreData.SQLDebug 1
. As you access the photo library, you’ll see Core Data logging debug information to the Console. -
If you look at a class dump of the Photos framework, you’ll see references to
NSManagedObjectID
and other Core Data types in the main classes, e.g.PHObject
has an_objectID: NSManagedObjectID
ivar.
Finding PhotoKit’s Core Data model
In my quest to understand the Photos framework better (especially its performance characteristics), I wanted to inspect its data model. I found a file named PhotoLibraryServices.framework/photos.momd/photos-10.0.mom
deep in the bowels of the Xcode 10.0 app bundle:1
/Applications/Xcode-10.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/photos.momd/photos-10.0.mom
Opening compiled Core Data models in Xcode
A .mom
file is a compiled Core Data model. Xcode can’t open this directly, but it can import one into another Core Data model. Follow these steps to view the model in Xcode:
- Create a new empty project. Xcode 10 doesn’t like to display Core Data model bundles outside of a project.
- Create a new empty “Core Data Data Model” file inside the project. This will create an
.xcdatamodeld
bundle. - Open the new data model and select Editor > Import…. Select the
.mom
file you want to import.
Unfortunately, the compiled model doesn’t store the layout information for Xcode’s graphical model editor, so you’ll have to manually drag the entities into a pleasing layout. This took me a few hours.2
The formatted Photos model
This is photos-10.0.mom
as bundled with Xcode 10.0 (iOS 12.0):
Not everything can be seen in this image. Download the full model and open it in Xcode to inspect attribute properties etc.
Note that this isn’t necessarily the entire data model for photos on iOS. There are more Core Data models in PrivateFrameworks/PhotoAnalysis.framework
which I ignored.
-
You can use this terminal command to find other Core Data models inside the simulator runtimes bundled with Xcode:
find /Applications/Xcode-10.app -name '*.mom'
-
Pro tip: you can use the arrow keys (and ⇧ + arrow keys) to position things precisely. Pro pro tip: don’t hit ⌘Z to undo a move operation. The graphical edits don’t register as undoable operations, so chances are Xcode will undo the initial import, which means you’ll lose all unsaved work. ↩︎