-
Notifications
You must be signed in to change notification settings - Fork 66
Example Snippets
This assumes that CouchDB is already running, on the default port, on the same computer. (Launching the Couchbase Server will take care of this.)
- (void) connectToDatabase {
CouchServer *server = [[CouchServer alloc] init];
CouchDatabase *database = [server databaseNamed: @"mydatabase"];
RESTOperation* op = [_database create];
if (![op wait] && op.httpStatus != 412) {
// failed to contact the server or create the database
// (a 412 status is OK; it just indicates the db already exists.)
}
}
For an example of this in action, look at DemoAppController.m.
This snippet requires that your app use the iOS Couchbase framework, which embeds a CouchDB server inside your iOS app. In this case the startup is asynchronous; the CouchbaseEmbeddedServer will call your -couchbaseDidStart: method when the server has started up, and you can then connect to it.
- (void) connectToDatabase {
CouchbaseEmbeddedServer* cb = [[CouchbaseEmbeddedServer alloc] init];
cb.delegate = self;
NSAssert([cb start], @"Couchbase couldn't start! Error = %@", cb.error);
}
- (void) couchbaseDidStart: (NSURL*)serverURL {
CouchServer *server = [[CouchServer alloc] init];
CouchDatabase *database = [server databaseNamed: @"mydatabase"];
RESTOperation* op = [_database create];
if (![op wait] && op.httpStatus != 412) {
// failed to contact the server or create the database
// (a 412 status is OK; it just indicates the db already exists.)
}
}
In this example we assume we don't have admin access to the remote server, so the database must already exist. To verify the connection we do a GET operation on the database URL; we ignore the result, just check for an error.
- (void) connectToDatabase {
NSURL* serverURL = [NSURL URLWithString: @"http://example.com:8080"];
CouchServer *server = [[CouchServer alloc] initWithURL: serverURL];
CouchDatabase *database = [server databaseNamed: @"theirdatabase"];
RESTOperation* op = [_database GET];
if (![op wait]) {
// failed to contact the server or access the database
}
}
CouchQuery* allDocs = database.allDocuments;
for (CouchQueryRow* row in allDocs.rows) {
CouchDocument* doc = row.document;
NSString* message = [doc.properties objectForKey: @"message"];
NSLog(@"Doc ID %@ has message: %@", row.documentID, message);
}
Here we get a document's current properties (contents), make a mutable copy of them and update that, then store the changed properties back to the database. Instead of calling -wait on the operation, we add an onCompletion handler block that will run in the future when it finishes. That allows the code here to finish immediately without blocking (sic) the user interface.
CouchRevision* latest = document.currentRevision;
NSMutableDictionary* props = [[latest.properties mutableCopy] autorelease];
int count = [[props objectForKey: @"count"] intValue];
count++;
[props setObject: [NSNumber numberWithInt: count] forKey: @"count"];
RESTOperation* op = [latest putProperties: props];
[op onCompletion: ^{
if (op.isSuccessful)
NSLog(@"Successfully updated document!");
else
NSLog(@"Failed to update document: %@", op.error);
}];