Preloading new data in iOS app updates in Swift

For many apps it is useful to preload data, either to provide examples for the user to reference, or as a starting place for them to work from. This article will not go over specifically how to load data, but rather how to add new data with an app update.

For example, an app might allow user to create categories but comes with some predefined. You (the developer) then later want to add more predefined categories. Or you add a new data type altogether in an update to the app and want to prepopulate data there.

Setup

The example app has a class called JSONController that has a static function preloadData() that loads data from a JSON file and stores it in CoreData. Again, this article will not be going over the process of loading data, so you should have a function for doing so prepared already.

App Delegate

To get started, we are going to create a function in App Delegate that will check the version number of the installed app and load the new data if there has been a update.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    preloadData()
    return true
}

private func preloadData() {

}

In the preloadData function, we will use UserDefaults to check the last app version that had data preloaded.

private func preloadData() {
    let preloadedDataKey = "preloadedDataVersion"
        
    let userDefaults = UserDefaults.standard
  
    let loadedVersion = userDefaults.string(forKey: preloadedDataKey)
}

loadedVersion will represent the last version that the user’s copy of the app preloaded data from. Note that this loadedVersion variable will be an optional string. If it’s the user’s first time opening the app, it will be nil.

Next we need to compare the version number that was stored in UserDefaults to the version number of the currently running app. If the current app version is later than loadedVersion, that means there’s been an app update, and it should try preloading any new data. Luckily for us, Swift has a function for comparing strings that are formatted as version numbers: String‘s compare function with the .numeric option.

private func preloadData() {
    let preloadedDataKey = "preloadedDataVersion"
        
    let userDefaults = UserDefaults.standard
  
    let loadedVersion = userDefaults.string(forKey: preloadedDataKey)
    if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
        (loadedVersion ?? "0").compare(appVersion, options: .numeric) == .orderedAscending {
        print("Preloading data...")
        JSONController.preloadData()
        userDefaults.set(appVersion, forKey: preloadedDataKey)
    }
}

The first line of this is getting the current app version from the Bundle. We then compare the last loaded version (loadedVersion, which we default to 0 if this is a new install) to the currently installed version. If the current version is ascending relative to the last version (if the current version is newer), then we run the contents of the block.

In this example, we print “Preloading data…” to the debug console just as a sanity check, then we run JSONController.preloadData(), this app’s function for preloading data. Replace this with whatever your app uses to preload data. After that, we store the current app version in UserDefaults so it won’t try to preload again until the next update.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: