Skip to content

Instantly share code, notes, and snippets.

@xavierchia
Last active March 18, 2024 05:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xavierchia/ef43abb270003ae63e5bbb7eb5404645 to your computer and use it in GitHub Desktop.
Save xavierchia/ef43abb270003ae63e5bbb7eb5404645 to your computer and use it in GitHub Desktop.
# In AppDelegate:
# This is to make sure that it only happens on the first launch
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let defaults = UserDefaults.standard
let isPreloaded = defaults.bool(forKey: "isPreloaded")
if !isPreloaded {
print("Preloading data for the first time")
preloadData()
defaults.set(true, forKey: "isPreloaded")
}
return true
}
# Preload the data
func preloadData() {
let sourceSqliteURLs = [Bundle.main.url(forResource: "DataModel", withExtension: "sqlite"), Bundle.main.url(forResource: "DataModel", withExtension: "sqlite-wal"), Bundle.main.url(forResource: "DataModel", withExtension: "sqlite-shm")]
let destSqliteURLs = [
URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/DataModel.sqlite"),
URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/DataModel.sqlite-wal"),
URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/DataModel.sqlite-shm")]
for index in 0...sourceSqliteURLs.count-1 {
do {
try FileManager.default.copyItem(at: sourceSqliteURLs[index]!, to: destSqliteURLs[index])
} catch {
print("Could not preload data")
}
}
}
@xavierchia
Copy link
Author

xavierchia commented Jan 23, 2021

Go here and click [download container], from the file go to Show Package Contents > App Data > Library > Application Support

Screenshot 2021-02-18 at 8 27 07 AM

Make sure you add the files to your folder and app bundle at Targets > Build Phases > Copy Bundle Resources. I personally like to put the source files in Model folder > Preload data folder

Screenshot 2021-01-23 at 8 09 22 AM

Don't forget to check Target Membership

Screenshot 2021-03-08 at 10 05 37 AM

@hartti
Copy link

hartti commented Jan 26, 2021

With SwiftUIApp, the process seems to be quite straightforward as well. The following approach seems to work for me.
If you are using the XCode-generated persistance model you have to replace this line

    let persistence = PersistenceManager()

in your main app fille with this

    let persistence: PersistenceManager
    
    init() {
        let defaults = UserDefaults.standard
        let isPreloaded = defaults.bool(forKey: "isPreloaded")
        if !isPreloaded {
            print("Preloading data for the first time")
// copy the contents of preloadData function here
            defaults.set(true, forKey: "isPreloaded")
        }
        persistence = PersistenceManager()
    }

@jetstreamaviator
Copy link

This is fantastic thank you!!!!!!! I've searched for hours for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment