From 71afee7335e4bdac9f56e6863496db0593c203b0 Mon Sep 17 00:00:00 2001 From: Chris Karr Date: Wed, 21 Jun 2017 13:19:46 -0500 Subject: [PATCH] Code cleanup and PDKHttpTransmitter refinements --- PassiveDataKit/PDKHttpTransmitter.m | 41 ++++++++++++++------------ PassiveDataKit/PDKLocationGenerator.m | 1 - PassiveDataKit/PassiveDataKit-Shared.h | 1 + 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/PassiveDataKit/PDKHttpTransmitter.m b/PassiveDataKit/PDKHttpTransmitter.m index 2998f93..c7cc60d 100644 --- a/PassiveDataKit/PDKHttpTransmitter.m +++ b/PassiveDataKit/PDKHttpTransmitter.m @@ -162,7 +162,7 @@ - (void) transmit:(BOOL) force completionHandler:(void (^)(UIBackgroundFetchResu } } - [self transmitReadings:0 completionHandler:completionHandler]; + [self transmitReadingsWithStart:[NSDate date].timeIntervalSince1970 completionHandler:completionHandler]; } - (NSURLRequest *) uploadRequestForPayload:(NSArray *) payload { @@ -180,13 +180,11 @@ - (NSUInteger) payloadSize { return 16; } -- (void) transmitReadings:(NSUInteger) uploadWindow completionHandler:(void (^)(UIBackgroundFetchResult result)) completionHandler { - if (uploadWindow == 0) { - uploadWindow = 8; //!OCLINT +- (void) transmitReadingsWithStart:(NSTimeInterval) start completionHandler:(void (^)(UIBackgroundFetchResult result)) completionHandler { + if (start < 1) { + start = [NSDate date].timeIntervalSince1970; //!OCLINT } - NSTimeInterval now = [NSDate date].timeIntervalSince1970; - sqlite3_stmt * statement = NULL; NSString * querySQL = [NSString stringWithFormat:@"SELECT D.id, D.properties FROM data D WHERE (D.timestamp < ?) LIMIT %d", (int) [self payloadSize]]; @@ -195,7 +193,7 @@ - (void) transmitReadings:(NSUInteger) uploadWindow completionHandler:(void (^)( if (sqlite3_prepare_v2(self.database, query_stmt, -1, &statement, NULL) == SQLITE_OK) { - sqlite3_bind_double(statement, 1, now); + sqlite3_bind_double(statement, 1, start); NSMutableArray * payload = [NSMutableArray array]; NSMutableArray * uploaded = [NSMutableArray array]; @@ -230,18 +228,13 @@ - (void) transmitReadings:(NSUInteger) uploadWindow completionHandler:(void (^)( NSURLRequest * request = [self uploadRequestForPayload:payload]; if (request != nil) { - NSLog(@"UPLOADING PAYLOAD: %@", payload); - PDKAFURLSessionManager * manager = [[PDKAFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { - if ([responseObject containsObject:@"Data bundle added successfully, and ready for processing."] == NO) { - NSLog(@"Invalid response: %@", responseObject); - } - else if (error == nil) { + if (error == nil) { for (NSNumber * identifier in uploaded) { sqlite3_stmt * deleteStatement = NULL; @@ -269,19 +262,31 @@ - (void) transmitReadings:(NSUInteger) uploadWindow completionHandler:(void (^)( } } - NSTimeInterval interval = [NSDate date].timeIntervalSince1970 - now; + NSTimeInterval interval = [NSDate date].timeIntervalSince1970 - start; - if (uploadWindow > 0 && interval > uploadWindow && uploaded.count > 0) { - [self transmitReadings:(uploadWindow - interval) completionHandler:completionHandler]; + if (interval < 5 && uploaded.count > 0) { + [self transmitReadingsWithStart:start completionHandler:completionHandler]; } else { if (completionHandler != nil) { completionHandler(UIBackgroundFetchResultNewData); } - + } + } else { + NSLog(@"Error: %@", error); + if (completionHandler != nil) { + completionHandler(UIBackgroundFetchResultFailed); } } }] resume]; + } else { + if (completionHandler != nil) { + completionHandler(UIBackgroundFetchResultNoData); + } + } + } else { + if (completionHandler != nil) { + completionHandler(UIBackgroundFetchResultFailed); } } } @@ -324,8 +329,6 @@ - (void) receivedData:(NSDictionary *) dataPoint forGenerator:(PDKDataGenerator) NSDictionary * toStore = [self processIncomingDataPoint:dataPoint forGenerator:dataGenerator]; if (toStore != nil) { - NSLog(@"STORING: %@", toStore); - sqlite3_stmt * stmt; NSString * insert = @"INSERT INTO data (timestamp, properties) VALUES (?, ?);"; diff --git a/PassiveDataKit/PDKLocationGenerator.m b/PassiveDataKit/PDKLocationGenerator.m index e2da633..6d21e1a 100644 --- a/PassiveDataKit/PDKLocationGenerator.m +++ b/PassiveDataKit/PDKLocationGenerator.m @@ -109,7 +109,6 @@ - (void) updateOptions:(NSDictionary *) options { } NSLog(@"TODO: Update options and refresh generator!"); - } diff --git a/PassiveDataKit/PassiveDataKit-Shared.h b/PassiveDataKit/PassiveDataKit-Shared.h index 71c213a..543c3dc 100644 --- a/PassiveDataKit/PassiveDataKit-Shared.h +++ b/PassiveDataKit/PassiveDataKit-Shared.h @@ -12,3 +12,4 @@ FOUNDATION_EXPORT const unsigned char PassiveDataKit_SharedVersionString[]; #import "PDKHttpTransmitter.h" #import "PDKAFURLRequestSerialization.h" +#import "PDKAFURLResponseSerialization.h"