我正在尝试向用户显示复制进度,但到目前为止我找不到检索此信息的方法.我正在使用iOS.我从复制对象了解changesCount和completedChangesCount,但是当复制正在运行时,changeCount不断增加,您无法轻松将其转换为百分比进度.知道可以做些什么吗?
最佳答案 如果你看看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;
}
}
迅速:
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;
}
}
注意:连续复制将无限期保持活动状态,监视进一步的更改并进行传输.因此,我建议您阅读不同类型的复制以及复制状态标志.