Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GCD instead of threads #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Appirater.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <UIAlertViewDelegate> {

Expand Down
89 changes: 39 additions & 50 deletions Appirater.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -116,8 +112,9 @@ - (void)showRatingAlert {
}

- (BOOL)ratingConditionsHaveBeenMet {
if (APPIRATER_DEBUG)
return YES;
#if APPIRATER_DEBUG
return YES;
#endif

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

Expand Down Expand Up @@ -168,8 +165,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])
{
Expand All @@ -185,8 +183,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
{
Expand Down Expand Up @@ -216,8 +215,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])
{
Expand All @@ -233,8 +233,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
{
Expand Down Expand Up @@ -262,76 +263,64 @@ @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 {
[Appirater appLaunched:YES];
}

+ (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 {
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];
}

+ (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 {
Expand Down