gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:51:35 25 4
gpt4 key购买 nike

我试图向用户显示复制进度,但到目前为止我找不到检索此信息的方法。我正在使用 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/

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