-
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 CouchbaseMobile object will call your -couchbaseMobile:DidStart: method when the server has started up, and you can then connect to it.
- (void) connectToDatabase {
CouchbaseMobile* cb = [[CouchbaseMobile alloc] init];
cb.delegate = self;
NSAssert([cb start], @"Couchbase couldn't start! Error = %@", cb.error);
}
- (void) couchbaseMobile: (CouchbaseMobile*)cb didStart: (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.)
}
}
-(void)couchbaseMobile: (CouchbaseMobile*)couchbase failedToStart: (NSError*)error {
NSLog(@"Couchbase couldn't start! Error = %@", error);
exit(1); // Put real failure handling here :)
}
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);
}
This example creates a CouchDB view (comparable to a stored SQL query) called "emailByName" that indexes all documents with a "name" property and associates the "email" property as the value. It then enumerates all the rows of the view/query (sorted by name) and writes the name and email.
CouchDesignDocument* design = [database designDocumentWithName: @"mydesign"];
[design defineViewNamed: @"emailByName" map: @"function(doc){if (doc.name) emit(doc.name,doc.email);};"];
CouchQuery* query = [design queryViewNamed: @"emailByName"];
for (CouchQueryRow* row in query.rows) {
NSLog(@"%@'s email is <%@>", row.key, row.value);
}
Note: The -defineViewNamed: method is smart enough to do nothing if the design document already contains a a view with the same name and function value, so it's OK to do the view creation right before you use the view; it won't cause extra database writes.
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);
}];