Reputation:
I am trying to get some really simple syncing between a couchbase lite mobile app and a couchbase server via a sync gateway. I have gotten the sync gateway to communicate with the server as using curl REST calls towards the gateway will sync with the main server.
However when attempting to sync with couchbase-lite, couchbase-lite simply does not sync.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LOG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the database (and create it if it doesn’t exist).
DatabaseConfiguration config = new DatabaseConfiguration(getApplicationContext());
Database database = null;
try {
database = new Database("mydb", config);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// Create a new document (i.e. a record) in the database.
MutableDocument mutableDoc = new MutableDocument()
.setFloat("version", 2.0F)
.setString("type", "SDK");
// Save it to the database.
try {
database.save(mutableDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// Update a document.
mutableDoc = database.getDocument(mutableDoc.getId()).toMutable();
mutableDoc.setString("language", "Java");
try {
database.save(mutableDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
Document document = database.getDocument(mutableDoc.getId());
// Log the document ID (generated by the database) and properties
Log.i(TAG, "Document ID :: " + document.getId());
Log.i(TAG, "Learning " + document.getString("language"));
// Create a query to fetch documents of type SDK.
Query query = QueryBuilder.select(SelectResult.all())
.from(DataSource.database(database))
.where(Expression.property("type").equalTo(Expression.string("SDK")));
ResultSet result = null;
try {
result = query.execute();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
Log.i(TAG, "Number of rows :: " + result.allResults().size());
// Create replicators to push and pull changes to and from the cloud.
Endpoint targetEndpoint = null;
try {
targetEndpoint = new URLEndpoint(new URI("ws://10.0.2.2:4984/demobucket"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
ReplicatorConfiguration replConfig = new ReplicatorConfiguration(database, targetEndpoint);
replConfig.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);
// Add authentication.
replConfig.setAuthenticator(new BasicAuthenticator("admin", "pass"));
// Create replicator.
Replicator replicator = new Replicator(replConfig);
// Listen to replicator change events.
replicator.addChangeListener(change -> {
if (change.getStatus().getError() != null) {
Log.i(TAG, "Error code :: " + change.getStatus().getError().getCode());
}
});
// Start replication.
replicator.start();
}
}
This code was literally pasted from the couchbase doc site https://docs.couchbase.com/couchbase-lite/current/java.html, yet does not work.
I get the error 11001, which equates to " // Peer has to close, e.g. because host app is quitting" which occurs in the replicator listener.
The sync gateway config file I use is as follows:
{
"interface":":4984",
"logging": {
"log_file_path": "/var/tmp/sglogs",
"console": {
"log_level": "debug",
"log_keys": ["*"]
},
"error": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 180
}
},
"warn": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 90
}
},
"info": {
"enabled": false
},
"debug": {
"enabled": false
}
},
"databases": {
"demobucket": {
"import_docs": "continuous",
"enable_shared_bucket_access":true,
"bucket":"demobucket",
"server": "http://cb-server:8091",
"username": "admin",
"password": "password",
"num_index_replicas":0,
"users":{
"GUEST": {"disabled":true},
"admin": {"password": "password", "admin_channels": ["*"]}
},
"revs_limit":20
}
}
}
Upvotes: 2
Views: 491
Reputation: 6715
@Jay has the answer, in his comment. Replicator replicator
is a local variable. As soon as the Activity
is stopped, the replicator is eligible for garbage collection. That appears to the peer as if the host was stopping.
Upvotes: 4