Last active
January 4, 2018 04:12
-
-
Save jwhitehorn/721c4e251b7130ab2f476bba11c9fda8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Sync.h" | |
| #import "NSData+MD5Digest.h" | |
| @implementation Sync | |
| + (Sync *) defaultWorker { | |
| //GCD singleton pattern: http://www.galloway.me.uk/tutorials/singleton-classes/ | |
| static Sync *defaultWorker = nil; | |
| static dispatch_once_t onceToken; | |
| dispatch_once(&onceToken, ^{ | |
| defaultWorker = [[self alloc] init]; | |
| }); | |
| return defaultWorker; | |
| } | |
| - (void) downloadUpdates { | |
| __weak id this = self; | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
| NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0]; | |
| NSString *checksum = nil; | |
| BOOL initialSync = YES; | |
| if([this isUpdatePending]){ | |
| NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"updated.db"]; | |
| NSData *contents = [NSData dataWithContentsOfFile:filePath]; | |
| checksum = [contents MD5HexDigest]; | |
| initialSync = NO; | |
| }else if([this hasPerformedInitialSync]){ | |
| NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"database.db"]; | |
| NSData *contents = [NSData dataWithContentsOfFile:filePath]; | |
| checksum = [contents MD5HexDigest]; | |
| initialSync = NO; | |
| } | |
| NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | |
| [request setHTTPMethod:@"GET"]; | |
| NSString *url = @"http://localhost:5000/api/updates/"; | |
| if(checksum){ | |
| url = [NSString stringWithFormat:@"%@?checksum=%@", url, checksum]; | |
| } | |
| [request setURL:[NSURL URLWithString:url]]; | |
| [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *err) { | |
| NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; | |
| if (statusCode == 304) { | |
| //no database update available | |
| return; | |
| }else if(statusCode == 200){ | |
| //updated database | |
| if(initialSync){ | |
| [data writeToFile:@"database.db" atomically:YES]; | |
| }else{ | |
| [data writeToFile:@"updated.db" atomically:YES]; | |
| } | |
| } | |
| }] resume]; | |
| }); | |
| } | |
| - (void) applyOutstandingUpdates { | |
| __weak id this = self; | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
| if(![this isUpdatePending]){ | |
| return; //no update pending | |
| } | |
| NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0]; | |
| NSString *updatePath = [documentsDirectoryPath stringByAppendingPathComponent:@"updated.db"]; | |
| NSString *dbPath = [documentsDirectoryPath stringByAppendingPathComponent:@"database.db"]; | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| NSError *err; | |
| [fileManager moveItemAtPath:updatePath toPath:dbPath error:&err]; | |
| }); | |
| } | |
| - (BOOL) isUpdatePending { | |
| NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0]; | |
| NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"updated.db"]; | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| return [fileManager fileExistsAtPath:filePath]; | |
| } | |
| - (BOOL) hasPerformedInitialSync { | |
| NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0]; | |
| NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"database.db"]; | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| return [fileManager fileExistsAtPath:filePath]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment