gpt4 book ai didi

android - Cloudant 连续复制 Android

转载 作者:行者123 更新时间:2023-11-30 01:14:21 26 4
gpt4 key购买 nike

我在我的应用程序中使用 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 方法。

关于android - Cloudant 连续复制 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com