Load existing user context (if any) (BITTelemetryContext)

This commit is contained in:
Christoph Wendt 2015-09-04 15:08:40 -07:00
parent bc36a90e26
commit 4cb7826ec9
2 changed files with 9 additions and 58 deletions

View File

@ -22,11 +22,6 @@ FOUNDATION_EXPORT NSString *const kBITApplicationWasLaunched;
*/ */
@interface BITTelemetryContext : NSObject @interface BITTelemetryContext : NSObject
/**
* A Dictionary which holds content of property list in memory.
*/
@property (nonatomic, strong) NSMutableDictionary *metaData;
/** /**
* The instrumentation key of the app. * The instrumentation key of the app.
*/ */

View File

@ -10,8 +10,7 @@
#import "BITPersistence.h" #import "BITPersistence.h"
#import "BITPersistencePrivate.h" #import "BITPersistencePrivate.h"
NSString *const kBITTelemetrySessionId = @"BITTelemetrySessionId"; NSString *const kBITUserMetaData = @"BITUserMetaData";
NSString *const kBITSessionAcquisitionTime = @"BITSessionAcquisitionTime";
static char *const BITContextOperationsQueue = "net.hockeyapp.telemetryContextQueue"; static char *const BITContextOperationsQueue = "net.hockeyapp.telemetryContextQueue";
@ -54,14 +53,10 @@ static char *const BITContextOperationsQueue = "net.hockeyapp.telemetryContextQu
BITOperation *operationContext = [BITOperation new]; BITOperation *operationContext = [BITOperation new];
NSMutableDictionary *restoredMetaData = [[_persistence metaData] mutableCopy]; BITUser *userContext = [self loadUser];
_metaData = restoredMetaData ?: @{}.mutableCopy;
_metaData[@"users"] = restoredMetaData[@"users"] ?: @{}.mutableCopy;
BITUser *userContext = [self userForDate:[NSDate date]];
if (!userContext) { if (!userContext) {
userContext = [self newUser]; userContext = [self newUser];
[self addUser:userContext forDate:[NSDate date]]; [self saveUser:userContext];
} }
BITLocation *locationContext = [BITLocation new]; BITLocation *locationContext = [BITLocation new];
@ -94,56 +89,17 @@ static char *const BITContextOperationsQueue = "net.hockeyapp.telemetryContextQu
}); });
} }
- (void)addUser:(BITUser *)user forDate:(NSDate *)date { - (void)saveUser:(BITUser *)user{
NSString *timestamp = [self unixTimestampFromDate:date ?: [NSDate date]]; NSDictionary *userMetaData = @{kBITUserMetaData : user};
[self.persistence persistMetaData:userMetaData];
__weak typeof(self) weakSelf = self;
dispatch_sync(self.operationsQueue, ^{
typeof(self) strongSelf = weakSelf;
NSMutableDictionary *users = strongSelf.metaData[@"users"];
users[timestamp] = user;
[self.persistence persistMetaData:strongSelf.metaData];
});
} }
- (BITUser *)userForDate:(NSDate *)date { - (BITUser *)loadUser{
NSString *timestamp = [self unixTimestampFromDate:date]; NSDictionary *metaData =[self.persistence metaData];
NSMutableDictionary *users = self.metaData[@"users"]; BITUser *user = [metaData objectForKey:kBITUserMetaData];
__block BITUser *user = nil;
__weak typeof(self) weakSelf = self;
dispatch_sync(self.operationsQueue, ^{
typeof(self) strongSelf = weakSelf;
NSString *userKey = [strongSelf keyForTimestamp:timestamp inDictionary:users];
user = users[userKey];
});
return user; return user;
} }
- (NSString *)unixTimestampFromDate:(NSDate *)date {
return [NSString stringWithFormat:@"%ld", (time_t) [date timeIntervalSince1970]];
}
- (NSString *)keyForTimestamp:(NSString *)timestamp inDictionary:(NSDictionary *)dict {
for(NSString *key in [self sortedKeysOfDictionay:dict]) {
if([timestamp doubleValue] >= [key doubleValue]) {
return key;
}
}
return nil;
}
- (NSArray *)sortedKeysOfDictionay:(NSDictionary *)dict {
NSMutableArray *keys = [[dict allKeys] mutableCopy];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^(id a, id b) {
return [b compare:a options:NSNumericSearch];
}];
return sortedArray;
}
#pragma mark - Network #pragma mark - Network
- (void)configureNetworkStatusTracking { - (void)configureNetworkStatusTracking {