Skip to content

Instantly share code, notes, and snippets.

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();
}
app.get('/api/updates/:model', function (req, res) {
dispatch_async(syncQueue, ^{
@interface Sync()
@property (nonatomic) dispatch_queue_t syncQueue;
@end
@implementation Sync
@synthesize syncQueue;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- (IBAction)doApplyUpdate:(id)sender {
[[Sync defaultWorker] applyOutstandingUpdates];
}
#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, ^{
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];
}