Skip to content

Instantly share code, notes, and snippets.

@bluebanboom
Last active December 17, 2015 04:49
Show Gist options
  • Save bluebanboom/5553520 to your computer and use it in GitHub Desktop.
Save bluebanboom/5553520 to your computer and use it in GitHub Desktop.
Run action only at current version first run. It is very easy to use. Just Call runOnce:action, specify the only key to the action and target.
//
// OPRunOnce.h
// Run action only at current version first run.
// It is very easy to use.
// Just Call runOnce:action, specify the only key to
// the action and target.
//
#import <Foundation/Foundation.h>
@interface OPRunOnce : NSObject
+ (void)runOnce:(NSString *)key action:(BOOL (^)(NSString *version))action;
+ (void)runOnceAsync:(NSString *)key action:(BOOL (^)(NSString *version))action;
@end
//
// OPRunOnce.m
// Run action only at current version first run.
// It is very easy to use.
// Just Call runOnce:action, specify the only key to
// the action and target.
//
#import "OPRunOnce.h"
#define kOPRunOnceKey @"OPRunOnceKey"
#define kOPRunOnceVersion @"OPRunOnceVersion"
@interface OPRunOnce ()
@property (nonatomic, strong) NSString *version;
@end
@implementation OPRunOnce
- (id)init
{
self = [super init];
if (self != nil) {
self.version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
id runOnceData = [userDefaults objectForKey:kOPRunOnceKey];
if (runOnceData == nil) {
[userDefaults setObject:@{kOPRunOnceVersion: _version} forKey:kOPRunOnceKey];
}
else {
if (![_version isEqualToString:[runOnceData objectForKey:kOPRunOnceVersion]]) {
[userDefaults setObject:@{kOPRunOnceVersion: _version} forKey:kOPRunOnceKey];
}
}
[userDefaults synchronize];
}
return self;
}
- (BOOL)actionAlreadyRun:(NSString *)key
{
BOOL ret = NO;
@synchronized(self) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
id runOnceData = [userDefaults objectForKey:kOPRunOnceKey];
if ([runOnceData objectForKey:key] == nil)
ret = NO;
else
ret = YES;
}
return ret;
}
- (void)saveActionState:(NSString *)key
{
@synchronized(self) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
id runOnceData = [userDefaults objectForKey:kOPRunOnceKey];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:runOnceData];
[dict setObject:[NSNumber numberWithBool:YES] forKey:key];
[userDefaults setObject:dict forKey:kOPRunOnceKey];
[userDefaults synchronize];
}
}
- (void)run:(NSString *)key action:(BOOL (^)(NSString *version))action
{
if ([self actionAlreadyRun:key]) {
return;
}
if (action(_version)) {
[self saveActionState:key];
}
}
+ (id)sharedInstance {
static OPRunOnce *sharedInstance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedInstance = [[OPRunOnce alloc] init];
});
return sharedInstance;
}
+ (void)runOnce:(NSString *)key action:(BOOL (^)(NSString *version))action
{
OPRunOnce *run = [OPRunOnce sharedInstance];
[run run:key action:action];
}
+ (void)runOnceAsync:(NSString *)key action:(BOOL (^)(NSString *version))action
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OPRunOnce *run = [OPRunOnce sharedInstance];
[run run:key action:action];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment