Handling app updates in iOS/macOS apps

No Comments

TL;DR: A solution is shown in which every app update step (one step per app version) is wrapped in its own class implementing a special AppUpdate protocol. On app launch, all classes implementing this protocol are loaded via reflection, instantiated, sorted by version number ascending and then each update step is executed if it hasn’t been already executed earlier.

One of the problems mobile engineers have to face is regular app updates. This problem is generally not new. However, we decided to present our solution to this problem because it may offer some interesting points for some.

Prior to the update, an app can have its state persisted in many ways and updating the app often requires transforming the existing data or creating new data. Two major problems exist in this scenario:

  1. we need to make sure update steps are executed even when a user skipped one or more app versions (ie user was on wild offline vacation, and in the meantime, two new app updates got released)
  2. we need to make sure update steps are executed only once (if update steps were executed every time user starts an app, it would be redundant and very likely break things)

The code demonstrating this approach is available on Bitbucket for both Swift and ObjC. Let’s quickly go through the major steps.

In the begining

Usually, we want application updates to be executed as soon as the app starts. The usual callback for that is didFinishLaunchingWithOptions in AppDelegate. So, in there we would add a line which does the app updating:

[AppUpdater performUpdateSteps];
AppUpdater.performUpdateSteps()

Simple as that. Also, for debugging reasons, we add the following in AppDelegate, to print the simulator app’s data folder in a console:

#if TARGET_IPHONE_SIMULATOR
    NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif
#if (arch(i386) || arch(x86_64)) && os(iOS)
    let dirs: [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
            FileManager.SearchPathDomainMask.allDomainsMask, true)
    print(dirs[dirs.endIndex - 1])
#endif

That way, we can easily track the Library/Preferences folder where the Preferences plist file is kept and delete it for easier manual testing of update steps.

AppUpdater

Now let’s look into what’s going on in AppUpdater since it does the majority of work.

Guided by SRP, we have a class for every version update. Therefore, executing app updates comes down to iterating through update class instances and executing its update step. An example is given in the code bellow:

ASTEach(updaters, ^(id <AppUpdate> updater) {
    if ([updater canExecuteUpdate])
    {
        [self performUpdateStepWithUpdater:updater];
    }
});
for updater in updaters {
    if updater.canExecuteUpdate() {
        performUpdateStepWithUpdater(updater)
    }
}

In case you’re wondering where the ASTEach in the ObjC example comes from, it’s from Asterism, a functional toolbelt for Objective-C.
Pretty simple. Bellow is the class diagram showing relations between AppUpdater and AppUpdate protocol which is implemented by specific updaters (v1.0.0, v1.1.0, etc.)

class diagram app updates

Each update step has its own unique identifier (more on that later). To make sure app updates execute only once, update step identifiers can be stored in UserDefaults. Before executing an update, the app would check if the update identifier already exists in UserDefaults, and only execute the update step if it doesn’t exist. All this gives us the code for an AppUpdater class:

@implementation AppUpdater
 
+ (void) performUpdateSteps
{
    NSArray* updaters = @[
        [AppUpdate_001_000_000 new],
        [AppUpdate_001_001_000 new]
    ];
 
    updaters = ASTSort(updaters, ^NSComparisonResult(id <AppUpdate> obj1, id <AppUpdate> obj2) {
        return [obj1.version compare:obj2.version];
    });
 
    ASTEach(updaters, ^(id <AppUpdate> updater) {
        if ([updater canExecuteUpdate])
        {
            [self performUpdateStepWithUpdater:updater];
        }
    });
}
 
+ (void) performUpdateStepWithUpdater:(id <AppUpdate> const)updater
{
    if (![NSUserDefaults.standardUserDefaults objectForKey:updater.version])
    {
        NSLog(@"▸ Performing update step: %@", updater.stepName);
        updater.updateBlock();
        [NSUserDefaults.standardUserDefaults setValue:updater.stepDescription forKey:updater.version];
        NSLog(@"▸ Finished update step: %@", updater.stepName);
        [NSUserDefaults.standardUserDefaults synchronize];
    }
}
 
@end
class AppUpdater {
 
    class func performUpdateSteps() {
        let updaters = [
            AppUpdate_001_000_000(),
            AppUpdate_001_001_000()
        ]
 
        for updater in updaters {
            if updater.canExecuteUpdate() {
                performUpdateStepWithUpdater(updater)
            }
        }
        UserDefaults.standard.synchronize()
    }
 
    class func performUpdateStepWithUpdater(_ updater: AppUpdate) {
        if (UserDefaults.standard.object(forKey: updater.stepName()) == nil) {
            print("▸ Performing update step \(updater.stepName())")
            updater.updateBlock()()
            UserDefaults.standard.setValue(updater.stepDescription(), forKey: updater.stepName())
            print("▸ Finished update step: \(updater.stepName())")
        }
    }
}

The method performUpdateStepWithUpdater executes an app update step if it determines (by looking up UserDefaults) that it hasn’t been already executed. App update instances are listed in the updaters array sorted in the order in which they should be executed. Update classes are conveniently named like AppUpdate_001_001_000 where the ‘001_001_000’ suffix describes the app version like 1.1.0, supporting versions up to 999.999.999. This makes it useful for sorting app update classes in the IDE project explorer tree, but it also paves the way for another approach: instead of listing all updater classes in the array like we did:

NSArray* updaters = @[
    [AppUpdate_001_000_000 new],
    [AppUpdate_001_001_000 new]
];
let updaters = [
    AppUpdate_001_000_000(),
    AppUpdate_001_001_000()
]

we can retrieve all classes implementing the AppUpdate protocol by reflection, so our performUpdateSteps method comes down to:

+ (void) performUpdateSteps
{
    NSArray<Class>* updateClasses = [Reflection classesImplementingProtocol:@protocol(AppUpdate)];
    NSArray* updaters = ASTMap(updateClasses, (id (^)(id)) ^id(Class updateClass) {
        return [updateClass new];
    });
 
    updaters = ASTSort(updaters, ^NSComparisonResult(id <AppUpdate> obj1, id <AppUpdate> obj2) {
        return [obj1.version compare:obj2.version];
    });
 
    ASTEach(updaters, ^(id <AppUpdate> updater) {
        if ([updater canExecuteUpdate])
        {
            [self performUpdateStepWithUpdater:updater];
        }
    });
}
class func performUpdateSteps() {
    let updateClasses = getClassesImplementingProtocol(p: AppUpdate.self)
    let updaters = updateClasses.map({ (updaterClass) -> AppUpdate in
        return updaterClass.alloc() as! AppUpdate
    }).sorted {
        $0.version() < $1.version()
    }
 
    for updater in updaters {
        if updater.canExecuteUpdate() {
            performUpdateStepWithUpdater(updater)
        }
    }
    UserDefaults.standard.synchronize()
}

Once classes are extracted and instantiated, they are sorted by app version to ensure proper order of update step execution. Extracting all classes implementing a given protocol happens in getClassesImplementingProtocol method in Reflection.swift or Reflection.h. The content of those files can be looked up on bitbucket.
As an effect, all the developer has to do in order to execute the new app update is to create a class implementing the AppUpdate protocol.

AppUpdate step

Every app update step has its own class implementing the AppUpdate protocol. An example implementation is given bellow:

@implementation AppUpdate_01_001_00
 
- (NSString* const) version
{
    return @"001-001-000";
}
 
- (BOOL const) canExecuteUpdate
{
    return SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(9);
}
 
- (NSString* const) stepName
{
    return @"SpotlightIndexing";
}
 
- (NSString* const) stepDescription
{
    return @"Index documents and photos in spotlight.";
}
 
- (void (^)(void)) updateBlock
{
    return ^{
        // iterate through all documents and photos and index them in spotlight
    };
}
 
@end
class AppUpdate_001_001_000: NSObject, AppUpdate {
    func version() -> String {
        return "001-001-000"
    }
 
    func canExecuteUpdate() -> Bool {
        if #available(iOS 9.0, *) {
            return true
        } else {
            return false
        }
    }
 
    func stepName() -> String {
        return "upd-\(version())"
    }
 
    func stepDescription() -> String {
        return "Index documents and photos in spotlight."
    }
 
    func updateBlock() -> (() -> Void) {
        return {
            // EXAMPLE: iterate through all documents and photos and index them in spotlight
        }
    }
}

version method returns a string in format ###-###-### which is used to sort updaters chronologically. canExecuteUpdate is a place where the update step can decide whether to be executed or not (ie it only makes sense to execute on certain iOS versions because it relies on API introduced in relevant iOS version).
stepName returns a String which needs to be unique across all update step instances. This is because stepName String is used as a key in UserDefaults where we track which steps have been executed so far.
stepDescription is a short description of what the update step does (usually one sentence). It is stored as a value in UserDefaults.
updateBlock returns the block which does the actual job of whatever the update step should do. Some examples include:

  • resetting cached images to force the app to retrieve new shiny images from the server
  • indexing content in Spotlight
  • database scheme update / data migration in case you don’t use CoreData (which you probably should), but use something like FMDB
  • complete removal of local DB and other data forcing app to retrieve everything from the server again

Benefits

  • By wrapping each update step into its own class keeps the code clean and makes unit-testing easier.
  • Adding a new update step boils down to creating one class which implements the AppUpdate protocol.

Alternatives

Alternatively to the class-based approach described in this article, a block-based approach can be used. Such an approach is used by MTMigration utility.

Useful links

https://en.wikipedia.org/wiki/Single_responsibility_principle
https://en.wikipedia.org/wiki/Reflection_(computer_programming)
https://github.com/mysterioustrousers/MTMigration

Tags

Marko Cicak

codecentric Novi Sad employee since April 2012. Working as part of the CenterDevice iOS and macOS development team.

Comment

Your email address will not be published. Required fields are marked *