Skip to content

Instantly share code, notes, and snippets.

@interface Sync()
@property (nonatomic) dispatch_queue_t syncQueue;
@end
@implementation Sync
@synthesize syncQueue;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
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];
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;
- (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];
- (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];
}
#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, ^{
#import <Foundation/Foundation.h>
@interface Sync : NSObject
+ (Sync *) defaultWorker;
- (void) downloadUpdates;
- (void) applyOutstandingUpdates;
- (BOOL) isUpdatePending;
- (BOOL) hasPerformedInitialSync;
@jwhitehorn
jwhitehorn / api.js
Last active December 25, 2017 20:13
//...
app.get('/api/updates/:model', function (req, res) {
var filter = {};
if(req.query.timestamp){
filter['updated_at'] = onionrm.gte(req.query.timestamp);
}
//...
[{
"id": 1,
"name": "Oaklake Trails Naturist Park",
"latitude": 35.8082099,
"longitude": -96.5649402,
"rating": 4,
"address": "24601 Milfay Rd",
"city": "Depew",
"state": "OK",
"zip_code": "74028",