Skip to content

Commit

Permalink
Mac demo: enable continuous sync, use real view, set created_at.
Browse files Browse the repository at this point in the history
  • Loading branch information
snej committed Dec 14, 2011
1 parent f4129ec commit 28545ea
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
4 changes: 1 addition & 3 deletions Demo-Mac/DemoAppController.h
Expand Up @@ -14,7 +14,7 @@
// and limitations under the License.

#import <Cocoa/Cocoa.h>
@class CouchDatabase, CouchReplication, DemoQuery, TDReplicator;
@class CouchDatabase, CouchReplication, DemoQuery;


/** Generic application delegate for simple Mac OS CouchDB demo apps.
Expand All @@ -30,8 +30,6 @@
DemoQuery* _query;
CouchReplication *_pull, *_push;
BOOL _glowing;

TDReplicator* _puller, *_pusher;
}

@property (retain) DemoQuery* query;
Expand Down
55 changes: 31 additions & 24 deletions Demo-Mac/DemoAppController.m
Expand Up @@ -16,11 +16,7 @@
#import "DemoAppController.h"
#import "DemoQuery.h"
#import "Test.h"
#import "TDDatabase.h"
#import "TDServer.h"
#import "TDURLProtocol.h"
#import "TDPuller.h"
#import "TDPusher.h"
#import "TouchDB.h"
#import <CouchCocoa/CouchCocoa.h>


Expand Down Expand Up @@ -50,16 +46,13 @@ - (void) applicationDidFinishLaunching: (NSNotification*)n {
exit(1);
}

#if 0
CouchServer *server = [[CouchServer alloc] init];
#else
TDServer* tdServer = [[TDServer alloc] initWithDirectory: @"/tmp/ShoppingDemo" error: nil];
TDServer* tdServer = [[[TDServer alloc] initWithDirectory: @"/tmp/ShoppingDemo" error: nil] autorelease];
NSAssert(tdServer, @"Couldn't create TDServer");
[TDURLProtocol setServer: tdServer];
[tdServer release];

NSURL* url = [NSURL URLWithString: @"touchdb:///"];
CouchServer *server = [[CouchServer alloc] initWithURL: url];
#endif

_database = [[server databaseNamed: dbName] retain];
[server release];

Expand All @@ -68,7 +61,29 @@ - (void) applicationDidFinishLaunching: (NSNotification*)n {
NSAssert(op.error.code == 412, @"Error creating db: %@", op.error);
}

CouchQuery* q = [_database getAllDocuments];

// Create a CouchDB 'view' containing list items sorted by date
TDDatabase* tdb = [tdServer existingDatabaseNamed: dbName];
[[tdb viewNamed: @"byDate"] setMapBlock: ^(NSDictionary* doc, TDMapEmitBlock emit) {
id date = [doc objectForKey: @"created_at"];
if (date) emit(date, doc);
} version: @"1"];

// ...and a validation function requiring parseable dates:
[tdb addValidation: ^(TDRevision* newRevision, id<TDValidationContext>context) {
if (newRevision.deleted)
return YES;
id date = [newRevision.properties objectForKey: @"created_at"];
if (date && ! [RESTBody dateWithJSONObject: date]) {
NSLog(@"Invalid date: %@", date);//TEMP
context.errorMessage = [@"invalid date " stringByAppendingString: date];
return NO;
}
return YES;
}];


CouchQuery* q = [[_database designDocumentWithName: @"default"] queryViewNamed: @"byDate"];
q.descending = YES;
self.query = [[[DemoQuery alloc] initWithQuery: q] autorelease];
self.query.modelClass =_tableController.objectClass;
Expand All @@ -81,25 +96,17 @@ - (void) applicationDidFinishLaunching: (NSNotification*)n {


- (void) startContinuousSyncWith: (NSURL*)otherDbURL {
#if 1
TDDatabase* db = [[TDURLProtocol server] databaseNamed: _database.relativePath];
_puller = [[TDReplicator alloc] initWithDB: db remote: otherDbURL push: NO continuous: NO];
_pusher = [[TDReplicator alloc] initWithDB: db remote: otherDbURL push: YES continuous: NO];
#else
_pull = [[_database pullFromDatabaseAtURL: otherDbURL
options: kCouchReplicationContinuous] retain];
_push = [[_database pushToDatabaseAtURL: otherDbURL
options: kCouchReplicationContinuous] retain];
#endif
_pull = [[_database pullFromDatabaseAtURL: otherDbURL options: kCouchReplicationContinuous] retain];
_push = [[_database pushToDatabaseAtURL: otherDbURL options: kCouchReplicationContinuous] retain];
}


- (IBAction) pull:(id)sender {
[_puller start];
//[_puller start];
}

- (IBAction) push:(id)sender {
[_pusher start];
//[_pusher start];
}


Expand Down
2 changes: 1 addition & 1 deletion Demo-Mac/ShoppingDemo-Info.plist
Expand Up @@ -27,6 +27,6 @@
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>SyncDatabaseURL</key>
<string>http://127.0.0.1:5984/demo-shopping</string>
<string>http://127.0.0.1:5984/grocery-sync</string>
</dict>
</plist>
1 change: 1 addition & 0 deletions Demo-Mac/ShoppingItem.h
Expand Up @@ -12,5 +12,6 @@

@property bool check; // bool is better than BOOL: it maps to true/false in JSON, not 0/1.
@property (copy) NSString* text;
@property (retain) NSDate* created_at;

@end
8 changes: 7 additions & 1 deletion Demo-Mac/ShoppingItem.m
Expand Up @@ -10,6 +10,12 @@

@implementation ShoppingItem

@dynamic check, text;
@dynamic check, text, created_at;

- (NSDictionary*) propertiesToSave {
if (self.created_at == nil)
self.created_at = [NSDate date];
return [super propertiesToSave];
}

@end

0 comments on commit 28545ea

Please sign in to comment.