Skip to content

Instantly share code, notes, and snippets.

@srinadh-k
Last active November 13, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srinadh-k/f9940f1a97b073d8221de1b646bc290b to your computer and use it in GitHub Desktop.
Save srinadh-k/f9940f1a97b073d8221de1b646bc290b to your computer and use it in GitHub Desktop.
func startReplicator() {
stopReplication()
let url = URL(string: AppURLs.kCouchbaseURL)!
let target = URLEndpoint(url: url)
let config = ReplicatorConfiguration(database: database!, target: target)
config.replicatorType = .pushAndPull
config.allowReplicatingInBackground = true
if let channel = ChannelDetails.shared.channel{
config.channels = [channel]
}
config.continuous = true
config.authenticator = BasicAuthenticator(username: CBConstants.kUsername, password: CBConstants.kPassword)
self.replicator = Replicator(config: config)
self.replicator?.resetCheckpoint()
self.replicator?.start()
// Database.setLogLevel(.verbose, domain: .replicator)
//self.databaseListener()
self.listener_token = self.replicator?.addChangeListener({ (change) in
if let error = change.status.error as NSError? {
Print.print("--------- Error code :: \(error.code) and des \(error.debugDescription)")
}
else{
let s = change.status
Print.print("------- PushPull Replicator: \(s.progress.completed)/\(s.progress.total), error: \(String(describing: s.error)), activity = \(s.activity)")
if s.progress.completed == 0 && s.progress.total == 0 {
//This is initial synch and No data is available on couchbase server for specific store.
if s.activity == .idle{
Print.print("--------App installed and no data from server")
NotificationCenter.default.post(name: .didSyncCompleted, object: nil, userInfo: nil)
}
}
else if s.progress.completed == s.progress.total {
Print.print("--------All records are done")
NotificationCenter.default.post(name: .didSyncCompleted, object: nil, userInfo: nil)
// s.activity == Replicator.ActivityLevel.idle
}
else {
Print.print("------Still in progrss \(s.progress.completed) in \(s.progress.total)")
}
}
})
}
func stopReplication() {
if let _replicator = replicator{
if let token = listener_token{
_replicator.removeChangeListener(withToken: token)
}
_replicator.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment