This document provides an overview of building mobile applications with Couchbase. It introduces Couchbase Lite, an embedded JSON database that can work locally or sync with Couchbase Server. It describes how to initialize Couchbase Lite, create and query documents and views, customize table cells, add and delete items, and set up continuous replications between devices and a remote server. The document also demonstrates how to model objects with the CBLModel class to map to Couchbase documents. Sample code is provided and two demo apps are referenced that showcase Couchbase across iOS and OSX platforms syncing to a shared database.
8. Couchbase Lite
Couchbase Lite is an embedded JSON database
that can work standalone, in a P2P network, or as a
remote endpoint for Couchbase Server.
Embedded
All IO is local
Syncs are transparent in background
Disconnected operation
NO PUSH
NO Network
CBL
Device 1
CBL
Device 2
CB
Server
Cluster 1
with
GW!
16. Database View
//RootController or CBModel
//Define a view that only returns documents of type ‘kRocketDocType’
CBLView* view = [db viewNamed: @"rockets"];
if (!view.mapBlock) {
// Register the map function, the first time we access the view:
[view setMapBlock: MAPBLOCK({
if ([doc[@"type"] isEqualToString:kRocketDocType])
emit(doc[@"name"], nil);
}) reduceBlock: nil version: @"1"];
}
Map/Reduce
Called on every document
Optional Reduce
17. Using Views in your UI
// Create a query showing rockets:
CBLLiveQuery* query = [[[database viewNamed:@"rockets"] query] asLiveQuery];
// Plug the query into the CBLUITableSource, which will use it to drive the table.
// (The CBLUITableSource uses KVO to observe the query's .rows
self.dataSource.query = query;
self.dataSource.labelProperty = @"name";
// Assumes something like this in your controller
@property (nonatomic) IBOutlet UITableView* tableView;
@property (nonatomic) IBOutlet CBLUITableSource* dataSource;
18. Customizing Table Cells
- (void)couchTableSource:(CBLUITableSource*)source
willUseCell:(UITableViewCell*)cell
forRow:(CBLQueryRow*)row
// Set the cell background and font:
// Configure the cell contents. Map function (above) copies the doc
properties into its value, so we can read them without having to load the
document.
NSDictionary* rowValue = row.value;
BOOL checked = [rowValue[@"check"] boolValue];
if (checked) {
cell.textLabel.textColor = [UIColor grayColor];
cell.imageView.image = [UIImage imageNamed:@"checked"];
} else {
cell.textLabel.textColor = [UIColor blackColor];
cell.imageView.image = [UIImage imageNamed: @"unchecked"];
}
// cell.textLabel.text is already set, thanks to setting up labelProperty
}
23. CBLModel
• Maps Objects to documents
@interface ASDRocket : CBLModel
+ (NSString*) docType;
@property (strong) NSString *name;
@property (assign) double diameter;
@property (assign) double weight;
@property (assign) double coefficientFriction;
/** Returns a query for all the rockets in a database. */
+ (CBLQuery*) queryRocketsInDatabase: (CBLDatabase*)db;
/** Returns a query for all the rockets with name in a database */
+ (CBLQuery*) findInDatabase:(CBLDatabase*)db byName:(NSString *)name;
@end
24. CBLModel
• No types in documents
• convention is to use type field
• Map functions can pick out docs with the right type
if (doc.type == “rocket”){
emit(doc.created, doc.text)
};