Search code examples
iosswiftcore-datadatabase-migrationseed

Best practice to seed/populate Core Data?


What is the right approach to seed Core Data database at the first launch?

I have sql db with precreated data.

I need to make this data available to the user at the app's first launch and then delete the seed data to free up space. The seed data is 86 MB file.

Currently, I store this seed file in the root of the project and do include it in the app bundle but the problem with this approach is what the bundle size is increasing and I have no possibility to delete anything from bundle so after the seed is copied to user space app size doubles and consumes extra 86mb from no more useful seed file from the bundle and that makes me a headache.

  • As I researched here is no possibility to delete anything from bundle in runtime
  • I only found couple references to 10year old articles which is currently 404 at apple website

What I am missing and what I am doing wrong ?

I have no possibility to put that seed at the backend and then make user download it.

enter image description here


Solution

  • There's no real solution to this problem. In practice you almost always just download the starter data from an online file or your API.

    There is no situation where that is impossible, since, the user has just now used an internet connection to get the app, so, you know they have a net connection, so just download the file from somewhere.


    FOOTNOTE:

    Just so you know, if you're new to iOS. CoreData is the most worthless and confused system Apple has ever made. Just FWIW note that

    1. You could instead use the newer "SwiftData". Which is almost as useless and confused, but at least it's more modern and has excitingly different problems

    2. All sensible projects just use SQLite, which is 100% built in to all iOS phones from the operating system up (and indeed Android phones!), and is a world's top 5 database. As a programmer you need to master SQL anyway. And on iSO you just use https://github.com/groue/GRDB.swift and it's trivial.

    In the past Realm was also popular since CoreData is garbage, it is very easy to use, however, I have never, ever, seen any point in using anything other than SQLite which is literally easiest and best. And of course, all your server side will be SQL anyway.

    PS CoreData as you may know is not really a database, per se. It is a "persistence layer" as Apple says.

    PPS If you're just doing a hobbyist project or such, just use Firebase and sync everything.

    Hope it helps