我已经使用realm本地和同步设置我的应用程序,一切似乎都很好。我最近实现了多个远程领域的同步进度。哪个也很好用。
然而,在更改了一些非同步进度特定代码之后,发生了奇怪的事情。对于一个50MB的数据库,我曾经得到5-6个通知,现在我只得到第一个和同步完成时。
我恢复到旧代码,同样的数据库,它正在收到多个通知。
过去几天我一直试图解决这个问题而没有运气。知道为什么会这样吗?
在一个名为BusinessManager
的单身人士中,我保留了这样的引用:
class BusinessManager {
static let shared = BusinessManager()
struct SyncNotificationToken {
let business: Business
let downloadToken: SyncSession.ProgressNotificationToken
let uploadToken: SyncSession.ProgressNotificationToken
}
private var syncTokens: [String:SyncNotificationToken] = [:]
}
将每次更改中的同步进度引用更新到域:
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)
}
}
}
每次更改后,我都会丢弃旧的进度并创建一个新的进度(我尝试持有引用并跳过创建,如果进度存在;这对我遇到的问题没有任何影响)。
每次收到进度通知时,我都会创建一个包含信息的新结构并通过通知发送(以前我使用了委托;但由于BusinessManager类是单例,我更喜欢使用Notification):
func updateBusinessProgresses() {
guard let businesses = remoteBusinesses else { return }
for 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 a reference so that we keep getting the progresses
syncTokens[business.uuid] = SyncNotificationToken(business: business, downloadToken: _downloadToken, uploadToken: _uploadToken)
}
}
我在同步进度的开始和结束时收到通知并且之间没有进度更新的原因是什么?
非常感谢任何指导。