ios - 如何计算 iOS 上 CouchbaseLite 的复制进度?

标签 ios couchbase-lite

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

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

最佳答案

如果您查看有关复制的 Couchbase lite-iOS 指南,有 Observing and monitoring replications .

你可以这样实现:
objective-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;
    }
}

swift :

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;
    }
}

注意:连续复制将无限期地保持事件状态,观察发生的进一步变化并传输它们。因此,我建议您通读不同类型的复制以及复制状态标志。

关于ios - 如何计算 iOS 上 CouchbaseLite 的复制进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32970186/

相关文章:

iphone - 如何从 uiimageview op uibutton 中删除图像

couchbase - 从 Couchbase Lite 推而不拉

java - 如何以JSON格式在couchbase lite android中插入数据

couchbase - 2017年couchbase使用Sqlite吗?

ios - UIAccessibilityLayoutChangedNotification 和 UIAccessibilityScreenChangedNotification 之间的实际区别?

ios - UISlider 不显示 Tract

ios - 是否可以选择多个 MKAnnotationViews?

java - 正则表达式查询出现致命信号 11 (SIGSEGV) 错误

c# - Couchbase Lite 数据库创建 C# .NET

ios - 在 iOS 设备之间共享数据