This driver includes building TouchDB and CouchCocoa to make and all in one sync engine that connects with Couchbase Syncpoint to do higher level APIs like sharing and channels.
In your ~/code
directory:
git clone --recursive git://github.com/couchbaselabs/Syncpoint-iOS.git
cd Syncpoint-iOS
open Syncpoint.xcworkspace
And XCode will come up and you'll be staring at some Objective-C.
Please direct your attention to the Syncpoint/Demo-iOS/DemoAppDelegate.m
, where we setup SyncpointClient
with our remote URL, and then ask it for a CouchCocoa CouchDatabase
using the databaseForChannelNamed
call.
Here is the code with error handling.
// needs a comment
NSLog(@"Setting up Syncpoint...");
NSURL* remoteURL = [NSURL URLWithString: kServerURLString];
NSError* error;
self.syncpoint = [[SyncpointClient alloc]
initWithRemoteServer: remoteURL
appId: kSyncpointAppId
error: &error];
if (error) {
[self showAlert: @"Syncpoint failed to start."
error: error fatal: YES];
return YES;
}
self.database = [syncpoint databaseForChannelNamed: @"grocery-sync"
error: &error];
if (!self.database) {
NSLog(@"error <%@>", error);
[self showAlert: @"Couldn't create local channel."
error: error fatal: YES];
return YES;
}
database.tracksChanges = YES;
NSLog(@"...using CouchDatabase at <%@>", self.database.URL);
// Tell the RootViewController:
RootViewController* root =
(RootViewController*)navigationController.topViewController;
[root useDatabase: database];
That is the code that instantiates Syncpoint. The code that triggers paring with the cloud is in /Syncpoint/Demo-iOS/ConfigViewController.m
So that's how you tie your app to a Syncpoint instance in the cloud. With decent error handling, even.
You might be wondering how you do data stuff with Syncpoint. This is really more of a CouchCocoa question.
Here is an example of how we save a document (we use the (onCompletion handler to asynchronously respond to query)):
// create the new document properties
NSDictionary* docData = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], @"check"
text, @"text",
nil];
// save the document
CouchDocument* doc = [database untitledDocument];
RESTOperation *op = [doc putProperties: docData];
// asynchronous handler
[op onCompletion: ^{
if (op.error) {
NSLog(@"REST error %@", op.dump);
}
// do your thing
[self savedDocument: doc];
}]
[op start];
We are active on the mailing list here: https://groups.google.com/forum/#!forum/mobile-couchbase
License is Apache 2.0.