This file contains 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
var CampgroundPhoto = db.define("campground_photos", { | |
photo_url: String, | |
campground_id: Number, | |
updated_at: Date | |
}, { | |
hooks: { | |
beforeSave: function(next){ | |
this.updated_at = new Date(); | |
next(); | |
} |
This file contains 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
app.get('/api/updates/:model', function (req, res) { |
This file contains 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
dispatch_async(syncQueue, ^{ |
This file contains 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
@interface Sync() | |
@property (nonatomic) dispatch_queue_t syncQueue; | |
@end | |
@implementation Sync | |
@synthesize syncQueue; |
This file contains 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
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
This file contains 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
- (IBAction)doApplyUpdate:(id)sender { | |
[[Sync defaultWorker] applyOutstandingUpdates]; | |
} |
This file contains 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, ^{ |
This file contains 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
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; |
This file contains 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
- (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]; | |
This file contains 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
- (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]; | |
} |