Skip to content

Instantly share code, notes, and snippets.

@sarah
Created November 17, 2011 15:31
Show Gist options
  • Save sarah/1373417 to your computer and use it in GitHub Desktop.
Save sarah/1373417 to your computer and use it in GitHub Desktop.
Syncing with a filter
************
AppDelegate.m
************
-(void) performRemoteSync
{
NSURL *remoteURL = [NSURL URLWithString:kRemoteSyncURL];
NSLog(@"Going to perform remote sync with: %@", remoteURL);
self.pullRequest = [self.localCouch replicationFromDatabaseAtURL:remoteURL];
[pullRequest setContinuous: YES];
[pullRequest setFilter:@"todays_question/questions"];
NSLog(@"pullRequest: %@", pullRequest);
[pullRequest addObserver:self forKeyPath:@"completed" options:0 context:NULL];
[self listQuestions];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == pullRequest)
{
unsigned completed = pullRequest.completed;
unsigned total = pullRequest.total;
NSLog(@"completed: %u; total: %u", completed, total);
NSLog(@"SYNC progress: %u / %u", completed, total);
if(total > 0 && total == completed)
{
NSLog(@"Done syncing!");
[pullRequest removeObserver:self forKeyPath:@"completed"];
}
}
}
************
DESIGN DOC ON REMOTE COUCH
************
{
"_id": "_design/todays_question",
"_rev": "14-a20357760359cfd96ba5aa635693168a",
"language": "javascript",
"views": {
"TodaysQuestion": {
"map": "function(doc) {if(doc.type == \"Daily Poll\" && doc.release_date){emit(doc.release_date, doc);}}"
}
},
"filters": {
"questions": "function(doc) {if(doc.release_date){return true;}else{return false;}}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment