Last active
March 8, 2025 12:15
-
-
Save xavierchia/ef43abb270003ae63e5bbb7eb5404645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is fantastic thank you!!!!!!! I've searched for hours for a solution.