iosswiftrealmmongodb-realm

How to get the date time the MongoDB Realm on client last synced?


MongoDB Realm sync data in the background, how to get the date-time the realm has been synced last time? Want to surface this information to the user to indicate the data is up to date.

There is an RLMSyncManager class but it does not seem to contain any information about the last time sync, https://docs.mongodb.com/realm-sdks/objc/latest/Classes/RLMSyncManager.html


Solution

  • Realm SyncSession object has a addProgressNotification method https://github.com/realm/realm-cocoa/blob/b606bfddaa0856489561727207e755659faa9a7e/RealmSwift/Sync.swift#L557 so a callback can be added there to update a local date property to store the last synced realm updated time.

    Realm user object contains a list of active sessions that can be accessed via user.allSessions and alternative is realm.syncSession.

            let realm = realmProvider.realm
    
            let progressTokenDownload = realm.syncSession?.addProgressNotification(
                for: .download,
                mode: .forCurrentlyOutstandingWork
            ) { progress in
                print(">>> DEBUG: [DCSettingsViewModel:checkRealmSyncingStatus] download", progress.fractionTransferred)
            }
    
            let progressTokenUpdate = realm.syncSession?.addProgressNotification(
                for: .upload,
                mode: .forCurrentlyOutstandingWork
            ) { progress in
                print(">>> DEBUG: [DCSettingsViewModel:checkRealmSyncingStatus] upload  ", progress.fractionTransferred)
            }
    
            if let progressTokenUpdate = progressTokenUpdate, let progressTokenDownload = progressTokenDownload {
                progressNotificationTokens.append(progressTokenUpdate)
                progressNotificationTokens.append(progressTokenDownload)
            }