Cloudant连续复制Android

时间:2016-06-28 16:18:07

标签: android synchronization cloudant

我在我的应用程序中使用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中添加此选项?

1 个答案:

答案 0 :(得分:1)

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