Replacing Core Data with SwiftData

I have an app that uses Core Data. I'm switching to SwiftData but it looks like the sqlite files are stored in separate places in the application file directory so my SwiftData files aren't reading the CoreData store. I'm not sure why it's not reading from the same location. Is there something I'm missing? Here's an example of the paths that I see when I write information to the debug console:

SwiftData Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/63E9B61D-64B8-4D2D-A02C-3C306688F354/Documents/[Data File Name].sqlite

Core Data Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/96A5961B-54DD-43A9-A4C3-661B439D91AE/Documents/[Data File Name].sqlite

I am not sure how you retrieved and printed the paths, but the UUID after .../Containers/Data/Application is the root folder name of an app sandbox, and so the paths you provided seem to be in different sandboxes (63E9B61D-64B8-4D2D-A02C-3C306688F354 vs. 96A5961B-54DD-43A9-A4C3-661B439D91AE), which means the logs may come from different processes. I can think of that happening in the following cases:

  • The logs came from a main app and its extension.
  • You ran your app, which printed the first log; you then removed the app from your simulator and ran it again, which printed the second log.

The latter seems unlikely, but if that's the case, the data store will be there if you don't remove the app.

In the former case, if you'd share a data store between your main app and its extensions, consider putting your data store in an app group container. For the more information, see the following resources:

In any case, you can tell SwiftData where the Core Data store is by creating your SwiftData model container with a model Configuration that has the Core Data store URL. See init(_:schema:url:allowsSave:cloudKitDatabase:) for more information.

If you don't know where the Core Data store is, and you are using NSPersistentContainer, the store should be in the default directory, and you can get it using defaultDirectoryURL.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Replacing Core Data with SwiftData
 
 
Q