Sync Replication Progress not working

I’m not sure if my observer is the problem as it is being triggered, but progress is still zero (0). I already tried two notifications as found in forums.

// Class property
private(set) var pull: CBLReplication!
let cblReplicationChange: Notification.Name

// Notificaiton test#1
self.pull = self.database.createPullReplication(kSyncGatewayUrl)
NotificationCenter.default.addObserver(self, selector: #selector(self.pullProgress), name: self.cblReplicationChange, object: self.pull)
NotificationCenter.default.post(name: self.cblReplicationChange, object: self.pull)

// Notificaiton test#2
NotificationCenter.default.addObserver(self, selector: #selector(Sync.pullProgress(_:)), name: NSNotification.Name.cblReplicationChange, object: self.pull)

Both tested notifications above are being triggered, but progress is still 0.

In pullProgress(), i print to xcode console the following:
pull.changesCount // this is still 0
pull.completedChangesCount // this is still 0

Sync log:
Initial log once Sync is triggered
CBLReplication[from http://dev.db.suites.digital:4984/sync/]: offline, progress = 0 / 0, err: (null)
Then after the initial log, I print to xcode console the changesCount and completedChangesCount, value is 0.

Then eventually printed in xcode console
CBLReplication[from http://dev.db.suites.digital:4984/sync/]: active, progress = 0 / 0, err: (null)
CBLReplication[from http://dev.db.suites.digital:4984/sync/]: active, progress = 0 / 5, err: (null)
CBLReplication[from http://dev.db.suites.digital:4984/sync/]: idle, progress = 5 / 5, err: (null)

While the above logs are being printed to console, changesCount and completedChangesCount were only called or printed once.

Not sure if this is connected…
When user is unauthorized, the error does not also print the expected result.
let pullLastError: NSError? = self.pull?.lastError as? NSError
print(pullLastError?.code) // result is nil
print(pullLastError?.localizedDescription) // result is nil
Although I can see the sync log CBLHTTP[401, <http...

Kindly help. Thanks!

The counts are just provided so you can display a progress bar; they’re not exact. What is it you want to know about the replication? If you want to find out when it completes, look at the status property.

1 Like

I need to:

  1. display the progress of the sync replication
  2. check if a user is unauthorised

Based from the examples, it is implemented using ios NSNotificationCenter or NotificationCenter. So I did that, result below:

  1. I’m getting 0 progress (changesCount and completedChangesCount)
    • based from Log, i can see that there is progress
  2. Sync error is nil when I know that the user is unauthorised
    • based from Log, I can see that the user is unauthorised

What is self.cblReplicationChange?

let pullLastError: NSError? = self.pull?.lastError as? NSError

Oh — I think I see the problem. self.pull?.lastError is of type NSError??, i.e. it’s doubly optional. So casting it to NSError always fails because that’s not its type.

(I might be wrong about being doubly optional; I haven’t done any Swift coding in months so I’m rusty. But it is at least singly optional, so the code is still wrong. Try removing as? NSError.)

1 Like

Hi @jens, it’s working now.
Due to the many testing I did, I lost track which one is the culprit. Anyway, below notification is working. I’m now able to get the progress, and i’m now able to get the 401 error…

NotificationCenter.default.addObserver(self, selector: #selector(pullProgress), name: NSNotification.Name.cblReplicationChange, object: self.pull)

Thanks a lot for the help =)