Application Launch
In this document
Each mobile environment has it's own style of application initialization. This is where you initialize resources that need to last the lifetime of the application. For instance, there are usually a few lines of code that initialize Couchbase Lite and your data model.
Open the database
Ask the manager for a handle on your database.
CBLManager *manager = [CBLManager sharedInstance];
NSError *error;
self.database = [manager databaseNamed:@"todos" error:&error];
if (error) {
NSLog(@"error getting database %@",error);
exit(-1);
}
No code example is currently available.private void initDatabase() {
try {
Manager.enableLogging(Log.TAG, Log.VERBOSE);
Manager.enableLogging(Log.TAG_SYNC, Log.DEBUG);
Manager.enableLogging(Log.TAG_QUERY, Log.DEBUG);
Manager.enableLogging(Log.TAG_VIEW, Log.DEBUG);
Manager.enableLogging(Log.TAG_DATABASE, Log.DEBUG);
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
Log.e(TAG, "Cannot create Manager object", e);
return;
}
try {
database = manager.getDatabase(DATABASE_NAME);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Cannot get Database", e);
return;
}
}
private void initDatabase() {
try {
Manager.enableLogging(Log.TAG, Log.VERBOSE);
Manager.enableLogging(Log.TAG_SYNC, Log.DEBUG);
Manager.enableLogging(Log.TAG_QUERY, Log.DEBUG);
Manager.enableLogging(Log.TAG_VIEW, Log.DEBUG);
Manager.enableLogging(Log.TAG_DATABASE, Log.DEBUG);
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
Log.e(TAG, "Cannot create Manager object", e);
return;
}
try {
database = manager.getDatabase(DATABASE_NAME);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Cannot get Database", e);
return;
}
}
No code example is currently available.
Register Model Classes
The database can automatically spawn model objects of the correct type with a few hints. (iOS specific)
[[self.database modelFactory] registerClass:[List class] forDocumentType:@"list"];
[[self.database modelFactory] registerClass:[Task class] forDocumentType:@"item"];
No code example is currently available.No code example is currently available.No code example is currently available.No code example is currently available.
Trigger sync
If the user is already logged in, we want to start pull and push replication. Otherwise we'll wait for them to log in before syncing.
- (void)setupCBLSync {
// this Sync Manager is part of the ToDoLite, you might perfer to implement yours with delegate
// callbacks instead of blocks.
_cblSync = [[CBLSyncManager alloc] initSyncForDatabase:_database withURL:[NSURL URLWithString:kSyncUrl]];
// Tell the Sync Manager to use Facebook for login.
_cblSync.authenticator = [[CBLFacebookAuthenticator alloc] initWithAppID:kFBAppId];
if (_cblSync.userID) {
// we are logged in, go ahead and sync
[_cblSync start];
} else {
// Application callback to create the user profile.
// this will be triggered after we call [_cblSync start]
[_cblSync beforeFirstSync:^(NSString *userID, NSDictionary *userData, NSError **outError) {
[self performSelectorOnMainThread:@selector(runBlock:) withObject:^{
[self updateMyLists:userID userData:userData outError:outError];
} waitUntilDone:YES];
}];
}
}
No code example is currently available.if (application.getCurrentUserId() != null) {
switch (application.getAuthenticationType()) {
case CUSTOM_COOKIE:
// since the user has already logged in before, assume that we
// can start sync using the persisted cookie. if it's expired,
// a message will be shown and the user can login.
startSyncWithStoredCustomCookie();
break;
case FACEBOOK:
loginWithFacebookAndStartSync();
break;
}
}
if (application.getCurrentUserId() != null) {
switch (application.getAuthenticationType()) {
case CUSTOM_COOKIE:
// since the user has already logged in before, assume that we
// can start sync using the persisted cookie. if it's expired,
// a message will be shown and the user can login.
startSyncWithStoredCustomCookie();
break;
case FACEBOOK:
loginWithFacebookAndStartSync();
break;
}
}
No code example is currently available.