The problem I am trying to solve is the documents not syncing immediately.
What is happening: I modified a document and saved it to Couchbase using database.saveDocument(mutableDoc). I would expect the change to be propagated almost instantly to the Sync Gateway, but it is not being propagated. I confirm that the modified document is not live because I do a GET of the Document via the Sync Gateway REST API.
I have noticed that the only way to “force” instant propagation is to move the app to the background in iOS (suspend?) and then move it back as the active app. Then I check the document via the REST API and the changes are there.
I would like to know if there’s any reason why this is happening (default behaviour? misconfiguration?) so that I can fix it, and/or also if there is a way to trigger the update of the SG manually; to force the document’s propagation from CBL to the SG.
Let me share with you the “timeline” of the logs. Logs are mainly through the ReplicatorListener and a Document change listener:
Document’s revision is rev: 98. (Checked via SG REST API).
I edit a field (last_name) of the document. The document change listener notices this change and outputs:
Refreshing ID: users::182
Document from REST API still says rev: 98.
Move the app to the background (via iOS task manager). Output from Replicator:
2019-10-01 12:28:35.206622-0500 Register[22266:333429] [Snapshotting] Snapshotting a view (0x7fdd03053400, UIKeyboardImpl) that is not in a visible window requires afterScreenUpdates:YES.
Busy transferring data
All documents synced
Busy transferring data
All documents synced
Replicator in offline state
All documents synced
Replicator in offline state
All documents synced
Connecting to Sync Gateway
All documents synced
Connecting to Sync Gateway
All documents synced
Document from REST API says it’s still in rev: 98.
Move app again to main view from background. Output from Replicator:
2019-10-01 12:29:02.900973-0500 Register[22266:336140] TIC Read Status [2:0x0]: 1:57
Busy transferring data
All documents synced
Busy transferring data
All documents synced
Busy transferring data
Documents 1632 still pending sync
Replicator in Idle state
All documents synced
Busy transferring data
All documents synced
Replicator in Idle state
All documents synced
Document from REST API updated to rev: 99 and contains the changes.
This logs doesn’t help understand what happens with the replicator syncing, when you save the document. Wondering whether the replicator is busy/idle/stopped during document saving. If we could, check the logs whats the status of replicator when you are saving document, that will help understand the issue more clearly.
Could you please start collecting the logs, before the document is saving. Stop collecting it when replicator is idle/stopped.
The replicator in theory is starting before and should be running for the whole duration of the app’s lifecycle.
I have this function set up for listening to replicator changes, but it doesn’t get triggered when I update the document. It only gets triggered when I move the app to the background (and foreground).
private func replicatorStatusListener(change: ReplicatorChange) {
let s = change.status
switch s.activity {
case .busy:
print("Busy transferring data")
case .connecting:
print("Connecting to Sync Gateway")
case .idle:
print("Replicator in Idle state")
case .offline:
print("Replicator in offline state")
case .stopped:
print("Completed syncing documents")
}
// Workarond for BUG :https://github.com/couchbase/couchbase-lite-ios/issues/1816.
if s.progress.completed == s.progress.total {
print("All documents synced")
}
else {
print("Documents \(s.progress.total - s.progress.completed) still pending sync")
}
}
What I did was made a sample project with the minimal replication functions (get from Database and save and then perform an action with a document change listener) and it was working there.
I think it may have something to do with my replicator because I do not get any output from the Replicator (as if it was turned off). Do you know if there is a guide for best practices for a project with Couchbase Lite?
sample mobile todo-app: Check the section for Introduction, Getting started on iOS(Sync), where you can see an example on how it structured the replication code.
I ended up looking through the documentation and rewrote the code from scratch basing myself off of this example and now it works.
In terms of what was wrong, I narrowed it down to the Replicator being stopped somewhere, but never figured out where. at least if it happens again I have a better idea after rewriting the code myself.
Thanks for the guidance @jayahari.vavachan and for working on that sample code that worked for me!