如何在iOS上的CouchbaseLite中计算复制进度?

时间:2015-10-06 12:37:37

标签: ios couchbase-lite

我正在尝试向用户显示复制进度,但到目前为止我找不到检索此信息的方法。我正在使用iOS。我从复制对象了解changesCount和completedChangesCount,但是当复制正在运行时,changeCount不断增加,您无法轻松将其转换为百分比进度。知道可以做些什么吗?

changeCount的文档: http://couchbase.github.io/couchbase-lite-ios/docs/html/interfaceCBLReplication.html#a8e2733855342bb6855df8d5b6a97ef81

2 个答案:

答案 0 :(得分:2)

如果你看一下关于复制的Couchbase lite-iOS指南,那就是Observing and monitoring replications

你可以这样实现:
目标C

[[NSNotificationCenter defaultCenter] addObserver: self
                     selector: @selector(replicationChanged:)
                         name: kCBLReplicationChangeNotification
                       object: push];
[[NSNotificationCenter defaultCenter] addObserver: self
                     selector: @selector(replicationChanged:)
                         name: kCBLReplicationChangeNotification
                       object: pull];
- (void) replicationChanged: (NSNotification*)n {
    // The replication reporting the notification is n.object , but we
    // want to look at the aggregate of both the push and pull.

    // First check whether replication is currently active:
    BOOL active = (pull.status == kCBLReplicationActive) || (push.status == kCBLReplicationActive);
    self.activityIndicator.state = active;
    // Now show a progress indicator:
    self.progressBar.hidden = !active;
    if (active) {
        double progress = 0.0;
        double total = push.changesCount + pull.changesCount;
        if (total > 0.0) {
            progress = (push.completedChangesCount + pull.completedChangesCount) / total;
        }
        self.progressBar.progress = progress;
    }
}

<强>夫特:

NSNotificationCenter.defaultCenter().addObserver(self,
    selector: "replicationChanged:",
    name: kCBLReplicationChangeNotification,
    object: push)
NSNotificationCenter.defaultCenter().addObserver(self,
    selector: "replicationChanged:",
    name: kCBLReplicationChangeNotification,
    object: pull)
func replicationProgress(n: NSNotification) {
    // The replication reporting the notification is n.object , but we
    // want to look at the aggregate of both the push and pull.

    // First check whether replication is currently active:
    let active = pull.status == CBLReplicationStatus.Active || push.status == CBLReplicationStatus.Active
    self.activityIndicator.state = active
    // Now show a progress indicator:
    self.progressBar.hidden = !active;
    if active {
        var progress = 0.0
        let total = push.changesCount + pull.changesCount
        let completed = push.completedChangesCount + pull.completedChangesCount
        if total > 0 {
            progress = Double(completed) / Double(total);
        }
        self.progressBar.progress = progress;
    }
}

注意:连续复制将无限期保持活动状态,监视进一步的更改并进行转移。因此,我建议您阅读不同类型的复制以及复制状态标志。

答案 1 :(得分:0)

更新Swift3

func setupCBL() {

    let manager = CBLManager.sharedInstance()
    CBLManager.enableLogging("Sync")

    do {
        db = try manager.databaseNamed("tiny")
    } catch {
        print("no database")
    }


    if let url = URL(string:serverURL){
        pull = db?.createPullReplication(url)

        push = db?.createPushReplication(url)
    }


    pull?.continuous       = true
    push?.continuous       = true

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(CBLCore.replicationChanged),
                                           name: NSNotification.Name.cblReplicationChange,
                                           object: push)
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(CBLCore.replicationChanged),
                                           name: NSNotification.Name.cblReplicationChange,
                                           object: pull)

    push?.start()
    pull?.start()



}

func replicationChanged(n:Notification)  {

    if (self.push?.status == CBLReplicationStatus.active || self.pull?.status == CBLReplicationStatus.active) {
        let completed   = (self.pull?.completedChangesCount)! + (self.push?.completedChangesCount)!
        let total       = (self.pull?.changesCount)! + (self.push?.changesCount)!
        print("---------------------- in progress ---------------- \(completed) \\ \(total)")
    } else {
        if let error = self.push?.lastError ?? self.pull?.lastError{
            print("Error with code \(error)")
        }

    }
}