Skip to content

Instantly share code, notes, and snippets.

@pwightman
Created March 19, 2013 22:16
Show Gist options
  • Save pwightman/5200642 to your computer and use it in GitHub Desktop.
Save pwightman/5200642 to your computer and use it in GitHub Desktop.
//
// MTMigration.m
// Tracker
//
// Created by Parker Wightman on 2/7/13.
// Copyright (c) 2013 Mysterious Trousers. All rights reserved.
//
#import "MTMigration.h"
#define MT_MIGRATION_LAST_VERSION_KEY @"MTMigration.lastMigrationVersion"
#define MT_MIGRATION_APP_VERSION_KEY @"MTMigration.appVersion"
@implementation MTMigration
+ (void) migrateToVersion:(NSString *)version block:(void (^)())migrationBlock {
// version > lastMigrationVersion && version <= appVersion
if ([version compare:[self lastMigrationVersion] options:NSNumericSearch] == NSOrderedDescending &&
[version compare:[self appVersion] options:NSNumericSearch] != NSOrderedDescending) {
migrationBlock();
#if DEBUG
NSLog(@"MTMigration: Migrating to version %@", version);
#endif
[self setLastMigrationVersion:version];
}
}
// NEW CODE
+ (void) migrateFromVersion:(NSString *)version block:(void (^)())migrationBlock {
if ([version isEqualToString:[self lastMigrationVersion]]) {
migrationBlock();
}
}
// END NEW CODE
+ (void) reset {
[self setLastMigrationVersion:nil];
}
+ (NSString *)appVersion {
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
+ (void) setLastMigrationVersion:(NSString *)version {
[[NSUserDefaults standardUserDefaults] setValue:version forKey:MT_MIGRATION_LAST_VERSION_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSString *) lastMigrationVersion {
NSString *res = [[NSUserDefaults standardUserDefaults] valueForKey:MT_MIGRATION_LAST_VERSION_KEY];
return (res ? res : @"");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment