Login and Sync
In this document
When the user logs in, the application can trigger sync using the credentials obtained during the login process. In the application, the logic gets a little hard to follow, as this code is also responsible for prompting a fresh login when sessions expire, etc. So we'll just look at a couple of example snippets. If you cloned the source code yourself as part of a Getting Started tutorial (iOS, PhoneGap, or Android) now might be a good time to browse it.
Trigger Authenticated Sync
- (void)defineSync {
pull = [_database createPullReplication:_remoteURL];
pull.continuous = YES;
push = [_database createPushReplication:_remoteURL];
push.continuous = YES;
[self listenForReplicationEvents: push];
[self listenForReplicationEvents: pull];
[_authenticator registerCredentialsWithReplications: @[pull, push]];
}
No code example is currently available.public void startReplicationSyncWithCustomCookie(String name, String value, String path, Date expirationDate, boolean secure, boolean httpOnly) {
Replication[] replications = createReplications();
Replication pullRep = replications[0];
Replication pushRep = replications[1];
pullRep.setCookie(name, value, path, expirationDate, secure, httpOnly);
pushRep.setCookie(name, value, path, expirationDate, secure, httpOnly);
pullRep.start();
pushRep.start();
Log.v(TAG, "Start Replication Sync ...");
}
public void startReplicationSyncWithCustomCookie(String name, String value, String path, Date expirationDate, boolean secure, boolean httpOnly) {
Replication[] replications = createReplications();
Replication pullRep = replications[0];
Replication pushRep = replications[1];
pullRep.setCookie(name, value, path, expirationDate, secure, httpOnly);
pushRep.setCookie(name, value, path, expirationDate, secure, httpOnly);
pullRep.start();
pushRep.start();
Log.v(TAG, "Start Replication Sync ...");
}
No code example is currently available.
Tag User Documents
The user may create lists and tasks without ever establishing a connection to the cloud host. If they
decide they want to sync, they can authenicate (in the example app we use Facebook but you can easily
do custom auth). Because the data was created before we had an authenticated user to connect it with,
documents are missing user_id tags, so a crucial step before we actually sync, is to tag
all the existing local data with the user_id.
(In our case we use the email address associated with the Facebook account)
+ (void) updateAllListsInDatabase: (CBLDatabase*)database withOwner: (Profile*)owner error: (NSError**)e {
CBLQueryEnumerator *myLists = [[List queryListsInDatabase:database] run:e];
if (*e) {
return;
}
for (CBLQueryRow* row in myLists) {
List* list = [List modelForDocument: row.document];
list.owner = owner;
[list save:e];
if (*e) {
return;
}
}
}
No code example is currently available.public static void assignOwnerToListsIfNeeded(Database database, Document user)
throws CouchbaseLiteException {
QueryEnumerator enumerator = getQuery(database).run();
if (enumerator == null)
return;
while (enumerator.hasNext()) {
Document document = enumerator.next().getDocument();
String owner = (String) document.getProperty("owner");
if (owner != null) continue;
Map<String, Object> properties = new HashMap<String, Object>();
properties.putAll(document.getProperties());
properties.put("owner", user.getId());
document.putProperties(properties);
}
}
public static void assignOwnerToListsIfNeeded(Database database, Document user)
throws CouchbaseLiteException {
QueryEnumerator enumerator = getQuery(database).run();
if (enumerator == null)
return;
while (enumerator.hasNext()) {
Document document = enumerator.next().getDocument();
String owner = (String) document.getProperty("owner");
if (owner != null) continue;
Map<String, Object> properties = new HashMap<String, Object>();
properties.putAll(document.getProperties());
properties.put("owner", user.getId());
document.putProperties(properties);
}
}
No code example is currently available.