stackyyflow stackyyflow - 3 months ago 14
Android Question

Cloudant replicate all data in android

I'm trying to have user registration for my android application. I'm able to successfully make user register and store their details in Cloudant. They can also login using the phone they had used to register.

However, when I try using another phone to login the account, it doesn't work. Is possible to replicate all data from Cloudant so that users can also login to other phones too? Here is my code:

public class CloudantConnect {

private static final String TAG = CloudantConnect.class.getSimpleName();
private static final String DATASTORE_DIRECTORY = "data";

private Datastore datastore;
private IndexManager indexManager;

private Replicator push_replicator;
private Replicator pull_replicator;

private Context context;
private final Handler handler;
private RegisterActivity register_listener;

public CloudantConnect(Context context, String datastore_name) {
this.context = context;

// Set up information within its own folder in the application
File path = this.context.getApplicationContext().getDir(DATASTORE_DIRECTORY, Context.MODE_PRIVATE);
DatastoreManager manager = new DatastoreManager(path.getAbsolutePath());

try {
this.datastore = manager.openDatastore(datastore_name);
} catch (DatastoreNotCreatedException e) {
Log.e(TAG, "Unable to open Datastore", e);
}

// Reach here if datastore successfully created
Log.d(TAG, "Successfully set up database at" + path.getAbsolutePath());

// Set up replicator objects
try {
this.reloadReplicationSettings();
} catch (URISyntaxException e) {
Log.e(TAG, "Unable to construct remote URI from configuration", e);
}

this.handler = new Handler(Looper.getMainLooper());

Log.d(TAG, "CloudantConnect set up " + path.getAbsolutePath());
}


/**
* Creates new document for user details database storage
* @param user to store user details into database
* @return document of user details stored
*/
public User createNewUserDocument(User user) {
MutableDocumentRevision revision = new MutableDocumentRevision();
revision.body = DocumentBodyFactory.create(user.asMap());

try {
BasicDocumentRevision created = this.datastore.createDocumentFromRevision(revision);
return User.fromRevision(created);
} catch (DocumentException e) {
return null;
}
}

/**
* Sets replication listener
*/
public void setReplicationListener(RegisterActivity listener) {
this.register_listener = listener;
}

/**
* Start push replication
*/
public void startPushReplication() {
if(this.push_replicator != null) {
this.push_replicator.start();
} else {
throw new RuntimeException("Push replication not set up correctly");
}
}

/**
* Start pull replication
*/
public void startPullReplication() {
if(this.pull_replicator != null) {
this.pull_replicator.start();
} else {
throw new RuntimeException("Pull replication not set up correctly");
}
}

/**
* Stop running replication
*/
public void stopAllReplication() {
if(this.push_replicator != null) {
this.push_replicator.stop();
}

if(this.pull_replicator != null) {
this.pull_replicator.stop();
}
}

/**
* Stop running replication and reloads replication settings
* from the app's preferences.
*/
public void reloadReplicationSettings() throws URISyntaxException {
this.stopAllReplication();

// Set up new replicator objects
URI uri = this.createServerURI();

// Push replication
PushReplication push = new PushReplication();
push.source = datastore;
push.target = uri;
push_replicator = ReplicatorFactory.oneway(push);
push_replicator.getEventBus().register(this);

// Pull replication
PullReplication pull = new PullReplication();
pull.source = uri;
pull.target = datastore;
pull_replicator = ReplicatorFactory.oneway(pull);
pull_replicator.getEventBus().register(this);

Log.d(TAG, "Set up replicators for URI:" + uri.toString());
}

/**
* Calls when replication is completed
*/
public void complete(ReplicationCompleted rc) {
handler.post(new Runnable() {
@Override
public void run() {
if(register_listener != null) {
register_listener.replicationComplete();
}
}
});
}

/**
* Calls when replication has error
*/
public void error(ReplicationErrored re) {
Log.e(TAG, "Replication error:", re.errorInfo.getException());
handler.post(new Runnable() {
@Override
public void run() {
if(register_listener != null) {
register_listener.replicationError();
}
}
});
}
}

Answer

It looks like you've got all the code there to do replication. Do you actually call startPullReplication() from somewhere?

If you want your complete and error callbacks to run when replication completes/fails, you will need to add the @Subscribe annotation on them both so they're triggered when the events are put on the EventBus.

Comments