Permalink
Browse files

Added -[CouchDocument refresh] for use with remote servers without tr…

…acksChanges

It will validate the cached currentRevision, if any, against the server and update it if it changed.
  • Loading branch information...
1 parent e425585 commit 38be6883042084c3b58efaefe7be9c9e13d629cd @snej snej committed Mar 29, 2013
Showing with 36 additions and 0 deletions.
  1. +7 −0 Couch/CouchDocument.h
  2. +20 −0 Couch/CouchDocument.m
  3. +9 −0 Test/Test_Couch.m
@@ -68,6 +68,13 @@
With CouchDB, deleted revisions are NOT included, so this method acts identically to -getConflictingRevisions. */
- (NSArray*) getLeafRevisions;
+/** Compares the cached current revision against the server by sending a conditional GET. This is useful if you're connected to a remote database (not TouchDB) and don't have .tracksChanges turned on.
+ There are a couple of possibilities:
+ 1. The CouchDocument doesn't currently have a cached currentRevision. It will do nothing (not even contact the server).
+ 2. The currentRevision is found to be still current.
+ 3. The currentRevision is out of date. Updates the currentRevision from the server response. */
+- (void) refresh;
+
#pragma mark PROPERTIES:
/** The contents of the current revision of the document.
View
@@ -130,6 +130,26 @@ - (void) loadCurrentRevisionFrom: (CouchQueryRow*)row {
}
+- (void) refresh {
+ if (!_currentRevision) {
+ [_currentRevisionID release];
+ _currentRevisionID = nil;
+ return;
+ }
+
+ NSString* eTag = [NSString stringWithFormat: @"\"%@\"", _currentRevisionID];
+ RESTOperation* op = [self sendHTTP: @"GET" parameters: @{@"If-None-Match": eTag}];
+ if (op.httpStatus == 304) // this blocks
+ return;
+
+ // We got a different revision, so make it the current one:
+ [_currentRevision autorelease];
+ _currentRevision = [[CouchRevision alloc] initWithOperation: op];
+ [_currentRevisionID autorelease];
+ _currentRevisionID = [_currentRevision.revisionID copy];
+}
+
+
- (NSArray*) getRevisionHistory {
RESTOperation* op = [self sendHTTP: @"GET"
parameters: [NSDictionary dictionaryWithObjectsAndKeys:
View
@@ -130,6 +130,15 @@ - (void) test02_CreateRevisions {
}
+- (void) test021_RefreshDocument {
+ CouchDocument* doc = [self createDocumentWithProperties: @{@"foo": @1}];
+ STAssertTrue(doc.currentRevisionID != nil, @"no cur revision ID after creating");
+ STAssertTrue(doc.currentRevision != nil, @"no cur revision after creating");
+ [doc refresh];
+ STAssertTrue(doc.currentRevisionID != nil, @"no cur revision ID after refresh");
+}
+
+
- (void) test03_SaveMultipleDocuments {
NSMutableArray* docs = [NSMutableArray array];
for (int i=0; i<5; i++) {

0 comments on commit 38be688

Please sign in to comment.