Skip to content

Commit

Permalink
Refactored unsent session duration
Browse files Browse the repository at this point in the history
Removed old deviceID zero-IDFA fixer redundant request
Switched to async file save for suspending
Refactored suspending for crash reporting
  • Loading branch information
erkanyildiz committed Oct 31, 2016
1 parent b8cc352 commit e78b325
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
30 changes: 5 additions & 25 deletions Countly.m
Expand Up @@ -10,9 +10,7 @@

@interface Countly ()
{
NSTimeInterval unsentSessionLength;
NSTimer *timer;
NSTimeInterval lastTime;
NSTimer* timer;
BOOL isSuspended;
}
@end
Expand All @@ -31,10 +29,6 @@ - (instancetype)init
{
if (self = [super init])
{
timer = nil;
isSuspended = NO;
unsentSessionLength = 0;

#if (TARGET_OS_IOS || TARGET_OS_TV)
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(didEnterBackgroundCallBack:)
Expand Down Expand Up @@ -126,7 +120,6 @@ - (void)startWithConfig:(CountlyConfig *)config
[CountlyAPM.sharedInstance startAPM];

timer = [NSTimer scheduledTimerWithTimeInterval:CountlyConnectionManager.sharedInstance.updateSessionPeriod target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
lastTime = NSDate.date.timeIntervalSince1970;

[CountlyConnectionManager.sharedInstance beginSession];

Expand Down Expand Up @@ -188,16 +181,10 @@ - (void)setCustomHeaderFieldValue:(NSString *)customHeaderFieldValue

- (void)onTimer:(NSTimer *)timer
{
if (isSuspended == YES)
if (isSuspended)
return;

NSTimeInterval currTime = NSDate.date.timeIntervalSince1970;
unsentSessionLength += currTime - lastTime;
lastTime = currTime;

int duration = unsentSessionLength;
[CountlyConnectionManager.sharedInstance updateSessionWithDuration:duration];
unsentSessionLength -= duration;
[CountlyConnectionManager.sharedInstance updateSession];

[CountlyConnectionManager.sharedInstance sendEvents];
}
Expand All @@ -210,14 +197,9 @@ - (void)suspend

[CountlyConnectionManager.sharedInstance sendEvents];

NSTimeInterval currTime = NSDate.date.timeIntervalSince1970;
unsentSessionLength += currTime - lastTime;
[CountlyConnectionManager.sharedInstance endSession];

int duration = unsentSessionLength;
[CountlyConnectionManager.sharedInstance endSessionWithDuration:duration];
unsentSessionLength -= duration;

[CountlyPersistency.sharedInstance saveToFileSync];
[CountlyPersistency.sharedInstance saveToFile];
}

- (void)resume
Expand All @@ -233,8 +215,6 @@ - (void)resume
}
#endif

lastTime = NSDate.date.timeIntervalSince1970;

[CountlyConnectionManager.sharedInstance beginSession];

isSuspended = NO;
Expand Down
4 changes: 2 additions & 2 deletions CountlyConnectionManager.h
Expand Up @@ -24,8 +24,8 @@
+ (instancetype)sharedInstance;

- (void)beginSession;
- (void)updateSessionWithDuration:(int)duration;
- (void)endSessionWithDuration:(int)duration;
- (void)updateSession;
- (void)endSession;

- (void)sendEvents;
- (void)sendUserDetails:(NSString *)userDetails;
Expand Down
26 changes: 22 additions & 4 deletions CountlyConnectionManager.m
Expand Up @@ -7,6 +7,9 @@
#import "CountlyCommon.h"

@interface CountlyConnectionManager()
{
NSTimeInterval lastSessionStartTime;
}
#if TARGET_OS_IOS
@property (nonatomic) UIBackgroundTaskIdentifier bgTask;
#endif
Expand Down Expand Up @@ -61,6 +64,7 @@ - (void)tick

[CountlyPersistency.sharedInstance removeFromQueue:firstItemInQueue];
[self tick];
return;
}

NSString* serverInputEndpoint = [self.host stringByAppendingString:@"/i"];
Expand Down Expand Up @@ -136,6 +140,8 @@ - (void)tick

- (void)beginSession
{
lastSessionStartTime = NSDate.date.timeIntervalSince1970;

NSString* queryString = [[self queryEssentials] stringByAppendingFormat:@"&begin_session=1&metrics=%@", [CountlyDeviceInfo metrics]];

NSString* optionalParameters = [CountlyCommon.sharedInstance optionalParameters];
Expand All @@ -147,18 +153,18 @@ - (void)beginSession
[self tick];
}

- (void)updateSessionWithDuration:(int)duration
- (void)updateSession
{
NSString* queryString = [[self queryEssentials] stringByAppendingFormat:@"&session_duration=%d", duration];
NSString* queryString = [[self queryEssentials] stringByAppendingFormat:@"&session_duration=%d", [self sessionLengthInSeconds]];

[CountlyPersistency.sharedInstance addToQueue:queryString];

[self tick];
}

- (void)endSessionWithDuration:(int)duration
- (void)endSession
{
NSString* queryString = [[self queryEssentials] stringByAppendingFormat:@"&end_session=1&session_duration=%d", duration];
NSString* queryString = [[self queryEssentials] stringByAppendingFormat:@"&end_session=1&session_duration=%d", [self sessionLengthInSeconds]];

[CountlyPersistency.sharedInstance addToQueue:queryString];

Expand Down Expand Up @@ -303,6 +309,18 @@ - (BOOL)isRequestSuccessful:(NSURLResponse *)response
return (code >= 200 && code < 300);
}

- (int)sessionLengthInSeconds
{
static double unsentSessionLength = 0.0;

NSTimeInterval currentTime = NSDate.date.timeIntervalSince1970;
unsentSessionLength += (currentTime - lastSessionStartTime);
lastSessionStartTime = currentTime;
int sessionLengthInSeconds = (int)unsentSessionLength;
unsentSessionLength -= sessionLengthInSeconds;
return sessionLengthInSeconds;
}

#pragma mark ---

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
Expand Down
12 changes: 6 additions & 6 deletions CountlyCrashReporter.m
Expand Up @@ -112,10 +112,13 @@ void CountlyExceptionHandler(NSException *exception, bool nonfatal)
{
if(!CountlyConnectionManager.sharedInstance.connection)
CountlyConnectionManager.sharedInstance.connection = NSURLSessionDataTask.new;
[Countly.sharedInstance suspend];
//NOTE: suspend method adds 'event' and 'end_session' requests to queue and starts them.
//NOTE: `sendEvents` and `endSession` calls adds `event` and `end_session` requests to queue and starts them.
// a dummy connection object is created to prevent these requests when app is about to terminate due to crash

[CountlyConnectionManager.sharedInstance sendEvents];
[CountlyConnectionManager.sharedInstance endSession];
[CountlyPersistency.sharedInstance saveToFileSync];

NSString *urlString = [NSString stringWithFormat:@"%@/i", CountlyConnectionManager.sharedInstance.host];
NSString *queryString = [[CountlyConnectionManager.sharedInstance queryEssentials] stringByAppendingFormat:@"&crash=%@", [crashReport JSONify]];

Expand All @@ -126,10 +129,7 @@ void CountlyExceptionHandler(NSException *exception, bool nonfatal)

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[[NSURLSession.sharedSession dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
if(error || ![CountlyConnectionManager.sharedInstance isRequestSuccessful:response])
{
Expand Down

0 comments on commit e78b325

Please sign in to comment.