From 9c660caa79d663132d0b0b0caaa03fd4504afe2a Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Fri, 22 Jul 2011 15:25:42 +0200 Subject: [PATCH 1/4] Use #ifs for APPIRATER_DEBUG instead of runtime check. --- Appirater.h | 4 ++-- Appirater.m | 35 +++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Appirater.h b/Appirater.h index 67f0a6bc..9b4b06c4 100644 --- a/Appirater.h +++ b/Appirater.h @@ -118,10 +118,10 @@ extern NSString *const kAppiraterDeclinedToRate; #define APPIRATER_TIME_BEFORE_REMINDING 1 // double /* - 'YES' will show the Appirater alert everytime. Useful for testing how your message + '1' will show the Appirater alert everytime. Useful for testing how your message looks and making sure the link to your app's review page works. */ -#define APPIRATER_DEBUG NO +#define APPIRATER_DEBUG 0 @interface Appirater : NSObject { diff --git a/Appirater.m b/Appirater.m index 0d2bb62d..623574cc 100644 --- a/Appirater.m +++ b/Appirater.m @@ -116,8 +116,9 @@ - (void)showRatingAlert { } - (BOOL)ratingConditionsHaveBeenMet { - if (APPIRATER_DEBUG) - return YES; +#if APPIRATER_DEBUG + return YES; +#endif NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; @@ -168,8 +169,9 @@ - (void)incrementUseCount { [userDefaults setObject:version forKey:kAppiraterCurrentVersion]; } - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER Tracking version: %@", trackingVersion); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER Tracking version: %@", trackingVersion); +#endif if ([trackingVersion isEqualToString:version]) { @@ -185,8 +187,9 @@ - (void)incrementUseCount { int useCount = [userDefaults integerForKey:kAppiraterUseCount]; useCount++; [userDefaults setInteger:useCount forKey:kAppiraterUseCount]; - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER Use count: %d", useCount); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER Use count: %d", useCount); +#endif } else { @@ -216,8 +219,9 @@ - (void)incrementSignificantEventCount { [userDefaults setObject:version forKey:kAppiraterCurrentVersion]; } - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER Tracking version: %@", trackingVersion); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER Tracking version: %@", trackingVersion); +#endif if ([trackingVersion isEqualToString:version]) { @@ -233,8 +237,9 @@ - (void)incrementSignificantEventCount { int sigEventCount = [userDefaults integerForKey:kAppiraterSignificantEventCount]; sigEventCount++; [userDefaults setInteger:sigEventCount forKey:kAppiraterSignificantEventCount]; - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER Significant event count: %d", sigEventCount); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER Significant event count: %d", sigEventCount); +#endif } else { @@ -306,15 +311,17 @@ + (void)appLaunched:(BOOL)canPromptForRating { - (void)hideRatingAlert { if (self.ratingAlert.visible) { - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER Hiding Alert"); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER Hiding Alert"); +#endif [self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO]; } } + (void)appWillResignActive { - if (APPIRATER_DEBUG) - NSLog(@"APPIRATER appWillResignActive"); +#if APPIRATER_DEBUG + NSLog(@"APPIRATER appWillResignActive"); +#endif [[Appirater sharedInstance] hideRatingAlert]; } From fb39fb8c3110eddae02e0ba1e4e17ef2f2f391b2 Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Fri, 22 Jul 2011 15:49:30 +0200 Subject: [PATCH 2/4] Use GCD instead of threads. Was previously creating a new thread for every call to +userDidSignifica ntEvent, +appEnteredForeground, and +appLaunched. --- Appirater.m | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/Appirater.m b/Appirater.m index 623574cc..0b0fad08 100644 --- a/Appirater.m +++ b/Appirater.m @@ -267,34 +267,26 @@ @implementation Appirater @synthesize ratingAlert; -- (void)incrementAndRate:(NSNumber*)_canPromptForRating { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - +- (void)incrementAndRate:(BOOL)canPromptForRating { [self incrementUseCount]; - if ([_canPromptForRating boolValue] == YES && + if (canPromptForRating == YES && [self ratingConditionsHaveBeenMet] && [self connectedToNetwork]) { [self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO]; } - - [pool release]; } -- (void)incrementSignificantEventAndRate:(NSNumber*)_canPromptForRating { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - +- (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating { [self incrementSignificantEventCount]; - if ([_canPromptForRating boolValue] == YES && + if (canPromptForRating == YES && [self ratingConditionsHaveBeenMet] && [self connectedToNetwork]) { [self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO]; } - - [pool release]; } + (void)appLaunched { @@ -302,11 +294,9 @@ + (void)appLaunched { } + (void)appLaunched:(BOOL)canPromptForRating { - NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating]; - [NSThread detachNewThreadSelector:@selector(incrementAndRate:) - toTarget:[Appirater sharedInstance] - withObject:_canPromptForRating]; - [_canPromptForRating release]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ + [[Appirater sharedInstance] incrementAndRate:canPromptForRating]; + }); } - (void)hideRatingAlert { @@ -326,19 +316,15 @@ + (void)appWillResignActive { } + (void)appEnteredForeground:(BOOL)canPromptForRating { - NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating]; - [NSThread detachNewThreadSelector:@selector(incrementAndRate:) - toTarget:[Appirater sharedInstance] - withObject:_canPromptForRating]; - [_canPromptForRating release]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ + [[Appirater sharedInstance] incrementAndRate:canPromptForRating]; + }); } + (void)userDidSignificantEvent:(BOOL)canPromptForRating { - NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating]; - [NSThread detachNewThreadSelector:@selector(incrementSignificantEventAndRate:) - toTarget:[Appirater sharedInstance] - withObject:_canPromptForRating]; - [_canPromptForRating release]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ + [[Appirater sharedInstance] incrementSignificantEventAndRate:canPromptForRating]; + }); } + (void)rateApp { From afbee36d61c7cc919f475860aa083966b85d488b Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Wed, 27 Jul 2011 17:29:47 +0200 Subject: [PATCH 3/4] Use GCD dispatch_once instead of @synchronized for +sharedInstance. This is supposed to be faster, see e.g. http://www.mikeash.com/pyblog/friday-qa-2009-09-18-intro-to-grand-central-dispatch-part-iv-odds-and-ends.html --- Appirater.m | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Appirater.m b/Appirater.m index 0b0fad08..32189884 100644 --- a/Appirater.m +++ b/Appirater.m @@ -92,15 +92,11 @@ - (BOOL)connectedToNetwork { + (Appirater*)sharedInstance { static Appirater *appirater = nil; - if (appirater == nil) - { - @synchronized(self) { - if (appirater == nil) { - appirater = [[Appirater alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:@"UIApplicationWillResignActiveNotification" object:nil]; - } - } - } + static dispatch_once_t appiraterOnceToken; + dispatch_once(&appiraterOnceToken, ^{ + appirater = [[Appirater alloc] init]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:@"UIApplicationWillResignActiveNotification" object:nil]; + }); return appirater; } From 143d06d71988e63dbcb4562324d9c9ac3a5a3749 Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Wed, 27 Jul 2011 18:01:14 +0200 Subject: [PATCH 4/4] Tweak: use UKKit constant instead of string. --- Appirater.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Appirater.m b/Appirater.m index 32189884..4e3eacea 100644 --- a/Appirater.m +++ b/Appirater.m @@ -95,7 +95,7 @@ + (Appirater*)sharedInstance { static dispatch_once_t appiraterOnceToken; dispatch_once(&appiraterOnceToken, ^{ appirater = [[Appirater alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:@"UIApplicationWillResignActiveNotification" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:UIApplicationWillResignActiveNotification object:nil]; }); return appirater;