2016-06-28 39 views
1

我在我的應用程序中使用IBM Cloudant。爲了連接到我的遠程數據庫我已經使用這個庫:https://github.com/cloudant/sync-android 。我試圖在Android中啓用連續複製,但我無法找到啓用它的方法。這是我使用的代碼:Cloudant連續複製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]; 

隨着我的代碼中,遠程數據庫在設備上本地複製,但兩個數據庫不連續同步。如果我在雲端控制檯上修改某些內容,則修改不會傳播到設備(而是在iOS中,它們會同步正確)。

有沒有辦法在Android中添加此選項?

回答

1

Cloudant Sync(您在android上使用的庫)不支持連續複製,因爲它們會影響電池壽命。如果您確實希望持續複製,那麼當您在public void complete(ReplicationCompleted event)方法中收到複製完成通知時,應該使用start方法重新啓動複製器。