-
-
Save srinadh-k/556194dafb53e1fd268bae68b76a7b88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func startReplicator(type: ReplicatorType) { | |
stopReplication() | |
let url = URL(string: AppURLs.kCouchbaseURL)! | |
let target = URLEndpoint(url: url) | |
let config = ReplicatorConfiguration(database: database!, target: target) | |
config.replicatorType = type | |
config.allowReplicatingInBackground = true | |
Print.print("Store ID \(StoreDetails.shared.storeID ?? "No Store ID")") | |
if let storeid = StoreDetails.shared.storeID{ | |
config.channels = [storeid, CBConstants.ChannelNames.kIndustrystandards] | |
} | |
config.continuous = true | |
config.authenticator = BasicAuthenticator(username: CBConstants.kUsername, password: CBConstants.kPassword) | |
self.replicator = Replicator(config: config) | |
self.replicator?.resetCheckpoint() | |
Print.print("================ Replication Going To Start =================") | |
self.replicator?.start() | |
Print.print("================ Replication Started =================") | |
Database.setLogLevel(.verbose, domain: .all) | |
//self.databaseListener() | |
listener_token = self.replicator?.addChangeListener({ (change) in | |
if change.status.activity == .stopped{ | |
print("Replication status is ============== stopped \(change.status)") | |
//self.startReplicator(type: type) | |
} | |
if let error = change.status.error as NSError? { | |
Print.print("--------- Error code :: \(error.code) and des \(error.debugDescription) and status is \(change.status.activity)") | |
// if let cberror = CBServerErrorCodes.init(rawValue: error.code){ | |
// Print.print("----------- CBErrro is \(cberror)") | |
// NotificationCenter.default.post(name: .CBServerStatus, object: ServerStatus.offline) | |
// } | |
if change.status.activity == .offline { | |
NotificationCenter.default.post(name: .CBServerStatus, object: ServerStatus.offline) | |
} | |
else if let cberror = CBServerErrorCodes.init(rawValue: error.code){ | |
NotificationCenter.default.post(name: .CBServerStatus, object: ServerStatus.offline) | |
} | |
} | |
else{ | |
let s = change.status | |
if s.activity == .connecting{ | |
Print.print("Trying to connect to Server....") | |
} | |
else{ | |
NotificationCenter.default.post(name: .CBServerStatus, object: ServerStatus.online) | |
} | |
Print.print("------- PushPull Replicator: \(s.progress.completed)/\(s.progress.total), error: \(String(describing: s.error)), activity = \(s.activity) and replicator \(change.replicator.config.replicatorType)") | |
if s.progress.completed == s.progress.total{ | |
Print.print("--------All records are done") | |
var userinfo : [String : Any] = [:] | |
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{ | |
userinfo[AppConstants.kType] = UserInfoType.EmptyData | |
self.replicator?.stop() | |
NotificationCenter.default.post(name: .didSyncCompleted, object: nil, userInfo: userinfo) | |
} | |
} | |
else{ | |
if AppPreferences.userDefaults.bool(forKey: UserDefaultsKeys.kIsInitialSyncFinish){ | |
userinfo[AppConstants.kType] = UserInfoType.OtherData | |
} | |
else{ | |
if change.replicator.config.replicatorType == ReplicatorType.push{ | |
userinfo[AppConstants.kType] = UserInfoType.MobileData | |
} | |
else if change.replicator.config.replicatorType == ReplicatorType.pull{ | |
userinfo[AppConstants.kType] = UserInfoType.CouchbaseData | |
} | |
else{ | |
userinfo[AppConstants.kType] = UserInfoType.OtherData | |
} | |
} | |
NotificationCenter.default.post(name: .didSyncCompleted, object: nil, userInfo: userinfo) | |
} | |
} | |
else { | |
if change.replicator.config.replicatorType == ReplicatorType.push && s.activity == Replicator.ActivityLevel.idle{ | |
var userinfo : [String : Any] = [:] | |
userinfo[AppConstants.kType] = UserInfoType.MobileData | |
NotificationCenter.default.post(name: .didSyncCompleted, object: nil, userInfo: userinfo) | |
} | |
let total = (Double(s.progress.completed)/(s.progress.total == 0 ? 1.0:Double(s.progress.total))) | |
Print.print("------Still in progrss \(s.progress.completed) in \(s.progress.total) percent \(total)") | |
if change.replicator.config.replicatorType == ReplicatorType.push{ | |
NotificationCenter.default.post(name: .isSyncProgress, object: nil, userInfo: [Notification.Name.isSyncProgress.rawValue:total, AppConstants.kType : UserInfoType.Optimizing ]) | |
} | |
else{ | |
NotificationCenter.default.post(name: .isSyncProgress, object: nil, userInfo: [Notification.Name.isSyncProgress.rawValue:total]) | |
} | |
} | |
} | |
}) | |
} | |
func databaseListener(){ | |
self.database!.addChangeListener({ (change) in | |
_ = change.documentIDs.map({ (documentid) -> Void in | |
Print.print("Documents ids are \(documentid)") | |
Print.print("Doc is ...... \(String(describing: self.database!.document(withID: documentid)?.toDictionary()))") | |
}) | |
}) | |
} | |
func stopReplication() { | |
if let _replicator = replicator{ | |
if let token = listener_token{ | |
_replicator.removeChangeListener(withToken: token) | |
} | |
Print.print("================ Replication Going To Stop =================") | |
_replicator.stop() | |
Print.print("================ Replication Stopped =================") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment