Created
March 19, 2013 22:16
-
-
Save pwightman/5200642 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// 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