#import <Foundation/Foundation.h>
#import <CouchCocoa/CouchCocoa.h>
#import <CouchCocoa/CouchTouchDBServer.h>
#import <TouchDB/TouchDB.h>
#define DB_NAME @"test"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Main bundle directory: %@", [NSBundle mainBundle].resourcePath);
// Connect to local Linux VM hosting CouchDB server
NSURL* testServerURL = [NSURL URLWithString: @"http://172.16.106.145:5984"];
CouchServer *testServer = [[CouchServer alloc] initWithURL:testServerURL];
CouchDatabase *testDB = [testServer databaseNamed: DB_NAME];
// Show all docs from production server
CouchQuery* allDocs = [testDB getAllDocuments];
NSLog(@"Test server has %ld docs. Printing all of their ID's:", allDocs.rows.count);
for (CouchQueryRow* row in allDocs.rows) {
NSLog(@"test server doc: %@", row.documentID);
}
// Create a TouchDB server
CouchTouchDBServer* tdbserver = [CouchTouchDBServer sharedInstance];
if (tdbserver.error) {
NSLog(@"ERROR: %@", [tdbserver.error localizedDescription]);
}
// Create database
CouchDatabase *localDB = [tdbserver databaseNamed:DB_NAME];
NSError *error = Nil;
if (![localDB ensureCreated:&error]) {
NSLog(@"ERROR: %@", [error localizedDescription]);
}
// Get some info about this server
NSLog(@"TouchDB databases: %@", tdbserver.getDatabases);
// Now replicate: pull from test server to touchdb
NSLog(@"Pulling from test to touchdb");
CouchReplication *pull = [localDB pullFromDatabaseAtURL:testDB.URL];
pull.continuous = FALSE;
RESTOperation *op = [pull start];
if (op != Nil) {
NSLog(@"Waiting for operation to finish: %d to replicate", pull.total);
[op wait];
NSLog(@"Operation has finished.");
}
allDocs = [localDB getAllDocuments];
NSLog(@"Printing all %ld docs in touchdb", allDocs.rows.count);
for (CouchQueryRow* row in allDocs.rows) {
NSLog(@"Test server doc: %@", row.documentID);
}
NSLog(@"Finished printing all docs in touchdb");
}
return 0;
}
2012-10-04 16:14:12.336 couchdb[19772:303] Main bundle directory: /Users/...blah blah blah...
2012-10-04 16:14:12.366 couchdb[19772:303] Test server has 5 docs. Printing all of their ID's:
2012-10-04 16:14:12.371 couchdb[19772:303] test server doc: c6613b51d3689329525e583511000ff3
2012-10-04 16:14:12.371 couchdb[19772:303] test server doc: c6613b51d3689329525e583511001021
2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e583511001e88
2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e5835110028f7
2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e583511003333
2012-10-04 16:14:12.383 couchdb[19772:303] TouchDB databases: (
"CouchTouchDBDatabase[_replicator]",
"CouchTouchDBDatabase[test]"
)
2012-10-04 16:14:12.384 couchdb[19772:303] Pulling from prod to test
2012-10-04 16:14:12.385 couchdb[19772:303] Waiting for operation to finish: 0 to replicate
2012-10-04 16:14:12.389 couchdb[19772:303] Operation has finished.
2012-10-04 16:14:12.392 couchdb[19772:303] Printing all 0 docs in touchdb
2012-10-04 16:14:12.397 couchdb[19772:303] Finished printing all docs in touchdb
#import <Foundation/Foundation.h>
#import <CouchCocoa/CouchCocoa.h>
#import <CouchCocoa/CouchTouchDBServer.h>
#import <TouchDB/TouchDB.h>
#define DB_NAME @"testdb"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Main bundle directory: %@", [NSBundle mainBundle].resourcePath);
// Connect to local Linux VM hosting CouchDB server
NSURL* testServerURL = [NSURL URLWithString: @"http://172.16.106.145:5984"];
CouchServer *testServer = [[CouchServer alloc] initWithURL:testServerURL];
CouchDatabase *testDB = [testServer databaseNamed: DB_NAME];
// Show all docs from production server
CouchQuery* allDocs = [testDB getAllDocuments];
NSLog(@"Test server has %ld docs. Printing all of their ID's:", allDocs.rows.count);
for (CouchQueryRow* row in allDocs.rows) {
NSLog(@"test server doc: %@", row.documentID);
}
// Create a TouchDB server
CouchTouchDBServer* tdbserver = [CouchTouchDBServer sharedInstance];
if (tdbserver.error) {
NSLog(@"ERROR: %@", [tdbserver.error localizedDescription]);
}
// Create touchdb database
CouchDatabase *localDB = [tdbserver databaseNamed:DB_NAME];
NSError *error = nil;
if (![localDB ensureCreated:&error]) {
NSLog(@"ERROR: %@", [error localizedDescription]);
}
// Now replicate: pull from test server to touchdb
NSLog(@"Pulling from test server to touchdb");
CouchReplication *pull = [localDB pullFromDatabaseAtURL:testDB.URL];
pull.continuous = FALSE;
RESTOperation *op = [pull start];
[op wait];
// start an explicit run loop to turn this async call to a sync call
NSTimeInterval timeout = 0.1;
while(timeout < 15.0 && pull.running){
NSLog(@"Waiting for touchdb to hurry the hell up!");
timeout += 0.1;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
NSLog(@"Replication has finished.");
allDocs = [localDB getAllDocuments];
NSLog(@"Printing all %ld docs in touchdb database", allDocs.rows.count);
for (CouchQueryRow* row in allDocs.rows) {
NSLog(@"Test server doc: %@", row.documentID);
}
NSLog(@"Finished printing all docs in touchdb");
}
return 0;
}
2012-10-05 16:58:22.755 couchdb[23762:303] Main bundle directory: /Users/.../blah blah blah
2012-10-05 16:58:22.795 couchdb[23762:303] Test server has 5 docs. Printing all of their ID's:
2012-10-05 16:58:22.804 couchdb[23762:303] test server doc: c6613b51d3689329525e583511000ff3
2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e583511001021
2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e583511001e88
2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e5835110028f7
2012-10-05 16:58:22.806 couchdb[23762:303] test server doc: c6613b51d3689329525e583511003333
2012-10-05 16:58:22.851 couchdb[23762:303] Pulling from test server to touchdb
2012-10-05 16:58:22.855 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:22.959 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.060 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.162 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.264 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.365 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.466 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.568 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.670 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.771 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.873 couchdb[23762:303] Waiting for touchdb to hurry the hell up!
2012-10-05 16:58:23.974 couchdb[23762:303] Replication has finished.
2012-10-05 16:58:23.977 couchdb[23762:303] Printing all 5 docs in touchdb database
2012-10-05 16:58:23.980 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511000ff3
2012-10-05 16:58:23.980 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511001021
2012-10-05 16:58:23.981 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511001e88
2012-10-05 16:58:23.981 couchdb[23762:303] Test server doc: c6613b51d3689329525e5835110028f7
2012-10-05 16:58:23.982 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511003333
2012-10-05 16:58:23.983 couchdb[23762:303] Finished printing all docs in touchdb
Thank you Jens! Got it working and attached my working code for others to see.
BTW Jens, you probably don't remember me but I spoke to you at the first CouchConf in San Francisco. I asked you about a more native stripped down solution for iOS instead of the Erlang based mobile solution. At the time it was just a twinkle in your eye, but it's great to see that you guys have committed to TouchDB and I'm sure that this will become an extremely popular and important mobile framework as it matures. Great job!