Import SQLite legacy data into CoreData

I'm currently rewriting an app developed over many years in Objective-C, Swift, SwiftUI, and an SQLIte database. Until now, I'd avoided CoreData because I'd been using SQL databases for decades (so was comfortable) and had concerns with CoreData's use of NS types. However, the new app will be multi-platform and share/sync data via CloudKit - which will be a pain in the derriere with SQLite (though not insurmountable). So, I'm going to do a test import of two related entities, 1,000 records and 4,500 records, to see how things go. There are more tables, but this will be just a test.

My intended process is:

  1. Export the SQLite tables, "main" and "details", separately, to CSV. There are linking IDs.
  2. In the new app, load each CSV file into DataFrames using the TabularData framework.
  3. For each row in the "main" DataFrame, filter the "details" DataFrame rows on the "main"'s ID, then create Core Data's "main" entity along with the associated "detail" entities.
  4. Do a context.save() - after each "main" or in batches?

Is there a better (less coding?) way?

My understanding is that I can't just populate each set of entities separately and then expect an automatic creation of the relationships. I've seen reference on the Web of Core Data "linking attributes", rather like joins in SQL, but I see no mention of such in the Xcode DataModel builder, nor class definitions. Am I correct in this understanding?

Regards, Michaela

The test import worked as planned, except (as mentioned by others elsewhere) for having to convert the "details" NSOrderedSet of "main" to a Swift Array for use in SwiftUI's ForEach. The import only took a second or two on an oldish iPad Pro in debug mode with Xcode 13 on a MacBook Pro (early 2015) - so that's OK. It looks like Fetch performance is good: List displays the 1000 or so "main" records, with their details, within a second - and that's on app launch.

The next step is to implement the CloudKit sync, and see what happens with syncing to other devices: i.e. syncing of the largish volumes of data. Any thoughts/suggestions?

Import SQLite legacy data into CoreData
 
 
Q