Skip to content

Commit

Permalink
Code cleanup and PDKHttpTransmitter refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Jun 21, 2017
1 parent af7dc30 commit 71afee7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
41 changes: 22 additions & 19 deletions PassiveDataKit/PDKHttpTransmitter.m
Expand Up @@ -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 {
Expand All @@ -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]];
Expand All @@ -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];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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 (?, ?);";
Expand Down
1 change: 0 additions & 1 deletion PassiveDataKit/PDKLocationGenerator.m
Expand Up @@ -109,7 +109,6 @@ - (void) updateOptions:(NSDictionary *) options {
}

NSLog(@"TODO: Update options and refresh generator!");

}


Expand Down
1 change: 1 addition & 0 deletions PassiveDataKit/PassiveDataKit-Shared.h
Expand Up @@ -12,3 +12,4 @@ FOUNDATION_EXPORT const unsigned char PassiveDataKit_SharedVersionString[];
#import "PDKHttpTransmitter.h"

#import "PDKAFURLRequestSerialization.h"
#import "PDKAFURLResponseSerialization.h"

0 comments on commit 71afee7

Please sign in to comment.