Realm Sync Progress (iOS / Swift) -
i have setup app realm local , sync, seems well. implemented sync progress multiple remote realms. working great also.
however, after changing not-sync progress specific code, weird thing happened. 50mb database used 5-6 notifications, first , when sync finished.
i revert old code, same db, getting multiple notifications.
i have been trying solve last couple of days no luck. idea why happen?
in singleton called businessmanager
, keep references this:
class businessmanager { static let shared = businessmanager() struct syncnotificationtoken { let business: business let downloadtoken: syncsession.progressnotificationtoken let uploadtoken: syncsession.progressnotificationtoken } private var synctokens: [string:syncnotificationtoken] = [:] }
update sync progress references in each change realm:
private func configureremotebusinessesnotification() { guard let remotebusinesses = remotebusinesses else { dlog("error obtaining remote businesses"); assert(false); return } remotebusinessesnotificationtoken?.stop() remotebusinessesnotificationtoken = remotebusinesses.addnotificationblock { changes in switch changes { case .initial: self.delegate?.loadedremotebusinesses() self.updatebusinessprogresses() case .update(let results, let deletions, let insertions, let modifications): self.delegate?.remotenotificationupdate(results: results, deletions: deletions, insertions: insertions, modifications: modifications) self.updatebusinessprogresses() case .error(let error): self.delegate?.notificationerror(error) } } }
after each change, discard old progress , create new 1 (i tried holding reference , skipped creating if progress exists; did not make difference in terms of problem having).
each time receive progress notification, create new struct holding info , send via notification (previously used delegate; since businessmanager class singleton, preferred using notification instead):
func updatebusinessprogresses() { guard let businesses = remotebusinesses else { return } business in businesses { guard let realmurl = business.realmself.configuration.syncconfiguration?.realmurl else { assert(false); return } guard let user = syncuser.current else { assert(false); return } guard let session = user.session(for: realmurl) else { assert(false); return } dlog("progress url: \(realmurl.description)") let uploadtoken = session.addprogressnotification(for: .upload, mode: .reportindefinitely, block: { progress in dlog("upload \(progress.transferrablebytes)") dispatchqueue.main.async { let object: [string:double] = [business.uuid:progress.fractiontransferred] dlog("upload \(business.uuid), fractiontransferred: \(int(progress.fractiontransferred*100))") notificationcenter.default.post(name: notification.upload.name, object: object) } }) let downloadtoken = session.addprogressnotification(for: .download, mode: .reportindefinitely, block: { progress in dlog("download \(progress.transferrablebytes)") dispatchqueue.main.async { let object: [string:double] = [business.uuid:progress.fractiontransferred] dlog("download \(business.uuid), fractiontransferred: \(int(progress.fractiontransferred*100))") notificationcenter.default.post(name: notification.download.name, object: object) } }) guard let _uploadtoken = uploadtoken, let _downloadtoken = downloadtoken else { assert(false); return } // hold reference keep getting progresses synctokens[business.uuid] = syncnotificationtoken(business: business, downloadtoken: _downloadtoken, uploadtoken: _uploadtoken) } }
what reason receiving notification @ start , @ end of sync progress , no progress update in between?
any guidance highly appreciated.
Comments
Post a Comment