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

Allow stopping location updates on status "285 Updates Not Required" #4

Merged
merged 1 commit into from
Aug 23, 2018
Merged
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
21 changes: 20 additions & 1 deletion BackgroundGeolocation/MAURBackgroundGeolocationFacade.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

FMDBLogger *sqliteLogger;

@interface MAURBackgroundGeolocationFacade () <MAURProviderDelegate>
@interface MAURBackgroundGeolocationFacade () <MAURProviderDelegate, MAURPostLocationTaskDelegate>
@end

@implementation MAURBackgroundGeolocationFacade {
Expand Down Expand Up @@ -87,6 +87,7 @@ - (instancetype) init
logger->setEnabled(YES);

postLocationTask = [[MAURPostLocationTask alloc] init];
postLocationTask.delegate = self;

localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
Expand Down Expand Up @@ -599,4 +600,22 @@ + (MAURLocationTransform _Nullable) locationTransform
return [MAURPostLocationTask locationTransform];
}

#pragma mark - MAURPostLocationTaskDelegate

- (void) postLocationTaskRequestedAbortUpdates:(MAURPostLocationTask *)task
{
if (_delegate && [_delegate respondsToSelector:@selector(onAbortRequested)])
{
// We have a delegate, tell it that there's a request.
// It will decide whether to stop or not.
[_delegate onAbortRequested];
}
else
{
// No delegate, we may be running in the background.
// Let's just stop.
[self stop:nil];
}
}

@end
13 changes: 12 additions & 1 deletion BackgroundGeolocation/MAURBackgroundSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@

#import <Foundation/Foundation.h>

@class MAURBackgroundSync;

@protocol MAURBackgroundSyncDelegate <NSObject>

@optional
- (void)backgroundSyncRequestedAbortUpdates:(MAURBackgroundSync * _Nonnull)task;

@end

@interface MAURBackgroundSync : NSObject

@property (nonatomic, weak) id<MAURBackgroundSyncDelegate> _Nullable delegate;

- (instancetype) init;
- (NSString*) status;
- (void) sync:(NSString*)url withTemplate:(id)locationTemplate withHttpHeaders:(NSMutableDictionary*)httpHeaders;
- (void) sync:(NSString * _Nonnull)url withTemplate:(id)locationTemplate withHttpHeaders:(NSMutableDictionary * _Nullable)httpHeaders;
- (void) cancel;

@end
Expand Down
24 changes: 20 additions & 4 deletions BackgroundGeolocation/MAURBackgroundSync.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)cancel
}
}

- (void) sync:(NSString*)url withTemplate:(id)locationTemplate withHttpHeaders:(NSMutableDictionary*)httpHeaders
- (void) sync:(NSString * _Nonnull)url withTemplate:(id)locationTemplate withHttpHeaders:(NSMutableDictionary * _Nullable)httpHeaders
{
MAURSQLiteLocationDAO* locationDAO = [MAURSQLiteLocationDAO sharedInstance];
NSArray *locations = [locationDAO getLocationsForSync];
Expand Down Expand Up @@ -132,20 +132,36 @@ - (NSString*)status


#pragma mark -
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{
DDLogInfo(@"Finished uploading task %zu %@: %@ %@, HTTP %ld", (unsigned long)[task taskIdentifier], task.originalRequest.URL, error ?: @"Success", task.response, (long)[(id)task.response statusCode]);
NSInteger statusCode = [(NSHTTPURLResponse *)task.response statusCode];

DDLogInfo(@"Finished uploading task %zu %@: %@ %@, HTTP %ld", (unsigned long)[task taskIdentifier], task.originalRequest.URL, error ?: @"Success", task.response, (long)statusCode);

[tasks removeObject:task];
NSURL *fullPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:task.taskDescription]];
[[NSFileManager defaultManager] removeItemAtURL:fullPath error:NULL];

if (statusCode == 285)
{
// Okay, but we don't need to continue sending these
DDLogDebug(@"Locations were uploaded to the server, and received an \"HTTP 285 Updates Not Required\"");

dispatch_async(dispatch_get_main_queue(), ^{
if (_delegate && [_delegate respondsToSelector:@selector(backgroundSyncRequestedAbortUpdates:)])
{
[_delegate backgroundSyncRequestedAbortUpdates:self];
}
});
}
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
DDLogInfo(@"Response:: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
{
DDLogError(@"Autosync failed :( %@", error);
}
Expand Down
14 changes: 12 additions & 2 deletions BackgroundGeolocation/MAURPostLocationTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@
#import "MAURConfig.h"
#import "MAURLocation.h"

@class MAURPostLocationTask;

@protocol MAURPostLocationTaskDelegate <NSObject>

@optional
- (void)postLocationTaskRequestedAbortUpdates:(MAURPostLocationTask * _Nonnull)task;

@end

@interface MAURPostLocationTask : NSObject

@property (nonatomic, weak) MAURConfig *config;
@property (nonatomic, weak) MAURConfig * _Nullable config;
@property (nonatomic, weak) id<MAURPostLocationTaskDelegate> _Nullable delegate;

- (void) add:(MAURLocation*)location;
- (void) add:(MAURLocation * _Nonnull)location;
- (void) start;
- (void) stop;
- (void) sync;
Expand Down
41 changes: 38 additions & 3 deletions BackgroundGeolocation/MAURPostLocationTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

static NSString * const TAG = @"MAURPostLocationTask";

@interface MAURPostLocationTask() <MAURBackgroundSyncDelegate>
{

}
@end

@implementation MAURPostLocationTask
{
Reachability *reach;
Expand All @@ -37,6 +43,7 @@ - (instancetype) init
hasConnectivity = YES;

uploader = [[MAURBackgroundSync alloc] init];
uploader.delegate = self;

reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability *_reach) {
Expand Down Expand Up @@ -65,7 +72,7 @@ - (void) stop
[reach stopNotifier];
}

- (void) add:(MAURLocation*)inLocation
- (void) add:(MAURLocation * _Nonnull)inLocation
{
// Take this variable on the main thread to be safe
MAURLocationTransform locationTransform = s_locationTransform;
Expand Down Expand Up @@ -130,12 +137,30 @@ - (BOOL) post:(MAURLocation*)location toUrl:(NSString*)url withTemplate:(id)loca
NSHTTPURLResponse* urlResponse = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:outError];

if ([urlResponse statusCode] == 200 || [urlResponse statusCode] == 201) {
NSInteger statusCode = urlResponse.statusCode;

if (statusCode == 285)
{
// Okay, but we don't need to continue sending these

DDLogDebug(@"Location was sent to the server, and received an \"HTTP 285 Updated Not Required\"");

dispatch_async(dispatch_get_main_queue(), ^{
if (_delegate && [_delegate respondsToSelector:@selector(postLocationTaskRequestedAbortUpdates:)])
{
[_delegate postLocationTaskRequestedAbortUpdates:self];
}
});
}

// All 2xx statuses are okay
if (statusCode >= 200 && statusCode < 300)
{
return YES;
}

if (*outError == nil) {
DDLogDebug(@"%@ Server error while posting locations responseCode: %ld", TAG, [urlResponse statusCode]);
DDLogDebug(@"%@ Server error while posting locations responseCode: %ld", TAG, (long)statusCode);
} else {
DDLogError(@"%@ Error while posting locations %@", TAG, [*outError localizedDescription]);
}
Expand All @@ -162,4 +187,14 @@ + (MAURLocationTransform _Nullable) locationTransform
return s_locationTransform;
}

#pragma mark - MAURBackgroundSyncDelegate

- (void)backgroundSyncRequestedAbortUpdates:(MAURBackgroundSync *)task
{
if (_delegate && [_delegate respondsToSelector:@selector(postLocationTaskRequestedAbortUpdates:)])
{
[_delegate postLocationTaskRequestedAbortUpdates:self];
}
}

@end
1 change: 1 addition & 0 deletions BackgroundGeolocation/MAURProviderDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef NS_ENUM(NSInteger, MAUROperationalMode) {
- (void) onLocationPause;
- (void) onLocationResume;
- (void) onActivityChanged:(MAURActivity*)activity;
- (void) onAbortRequested;
- (void) onError:(NSError*)error;

@end
Expand Down