Cloudant 连续复制 Android

Cloudant continuos replication Android

我在我的应用程序中使用 IBM Cloudant。为了连接到我的远程数据库,我使用了这个库:https://github.com/cloudant/sync-android .我试图在 Android 中启用连续复制,但我找不到启用它的方法。这是我正在使用的代码:

File path = getApplicationContext().getDir("datastores", Context.MODE_PRIVATE);
DatastoreManager manager = new DatastoreManager(path.getAbsolutePath());

 try {
        Datastore ds = manager.openDatastore("mydatabase");
        IndexManager indexManager = new IndexManager(ds);  

    URI uri = new URI(myuri);
    // Create a replicator that replicates changes from the remote
    // database to the local datastore.
    Replicator replicator = ReplicatorBuilder.pull().from(uri).to(ds).build();
    // Use a CountDownLatch to provide a lightweight way to wait for completion
    CountDownLatch latch = new CountDownLatch(1);
    Listener listener = new Listener(latch);
    replicator.getEventBus().register(listener);
    replicator.start();
    latch.await();
    replicator.getEventBus().unregister(listener);
    if (replicator.getState() != Replicator.State.COMPLETE) {
        System.out.println("Error replicating FROM remote");
        System.out.println(listener.error);
    }

  } catch (DatastoreException datastoreException) {

    System.err.println("Problem opening datastore: "+datastoreException);

  } catch (Exception documentException) {

    System.err.println("Problem: "+documentException);
}

监听器定义如下:

private class Listener {
    private final CountDownLatch latch;
    public ErrorInfo error = null;
    public int documentsReplicated;
    public int batchesReplicated;

    Listener(CountDownLatch latch) {
        this.latch = latch;
    }
    @Subscribe
    public void complete(ReplicationCompleted event) {
        this.documentsReplicated = event.documentsReplicated;
        this.batchesReplicated = event.batchesReplicated;
        latch.countDown();
    }
    @Subscribe
    public void error(ReplicationErrored event) {
        this.error = event.errorInfo;
        latch.countDown();
    }
}

在 IOS 中,我使用了类似的代码:

 CBLReplication *pullReplication = [_cblDatabase createPullReplication:synchronizationURL];

 //THIS IS THE PART THAT I WOULD LIKE TO HAVE IN ANDROID
 pullReplication.continuous = YES;

 [pullReplication start];

使用我的代码,远程数据库在设备上本地复制,但两个数据库没有连续同步。如果我在 cloudant 控制台上修改某些内容,则修改不会传播到设备(相反,在 iOS 中,它们会正确同步)。

有没有办法在 Android 中也添加此选项?

Cloudant Sync(您在 android 上使用的库)不支持连续复制,因为它们会影响电池寿命。如果您真的希望进行连续复制,当您在 public void complete(ReplicationCompleted event) 方法中收到复制完成通知时,您应该使用 start 方法重新启动复制器。