Realm-cocoa: SyncSession.addProgressNotification does not work

Created on 2 Feb 2017  Â·  8Comments  Â·  Source: realm/realm-cocoa

Goals

Get notifications for sync upload and download progress.

Expected Results

Get sync progress notifications.

Actual Results

Sync progress notifications are never delivered.

Steps to Reproduce

What are steps we can follow to reproduce this issue?

Code Sample

import UIKit
import RealmSwift

class O: Object {

    dynamic var data = Data()

}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var token: NotificationToken?

    var realm: Realm!
    var downloadToken: SyncSession.ProgressNotificationToken?
    var uploadToken: SyncSession.ProgressNotificationToken?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let serverURL = URL(string: "http://localhost:9080")!

        SyncUser.logIn(with: .usernamePassword(username: "username", password: "password"), server: serverURL) {
            assert($1 == nil)
            self.loginCompleted(user: $0!)
        }

        return true
    }

    func loginCompleted(user: SyncUser) {
        let realmURL = URL(string: "realm://localhost:9080/~/10k_data_multiple_writes")!
        var configuration = Realm.Configuration.defaultConfiguration
        configuration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmURL)
        realm = try! Realm(configuration: configuration)

        let session = user.session(for: realmURL)!
        downloadToken = session.addProgressNotification(for: .download, mode: .reportIndefinitely) {
            print("download progress: \($0)") // This is never called.
        }
        uploadToken = session.addProgressNotification(for: .upload, mode: .reportIndefinitely) {
            print("upload progress: \($0)") // This is never called.
        }

        for _ in 0..<10_000 {
            try! realm.write {
                let o = O()
                o.data = "this is a test with some data...".data(using: .utf8)!
                realm.add(o)
            }
        }
    }

}

Version of Realm and Tooling

Realm version: 2.4.2 / Realm Mobile Platform 1.0.0-2

Xcode version: 8.2.1

T-Bug

Most helpful comment

Sorry about your issue. It turns out there is a bug; to work around it for now you can dispatch the addProgressNotification calls to the main queue.

All 8 comments

Hi @anlaital. Thanks for reporting this. One of our engineers are looking into it.

Sorry about your issue. It turns out there is a bug; to work around it for now you can dispatch the addProgressNotification calls to the main queue.

(@austinzheng: adding the gist/sumary of our slack chat here to capture the other manifestation of this issue so it's part of the solution history)

DS: Am seeing the same issue - It feels like a possible race condition - My code is almost exactly the same - the realm has just been connected but I am guessing there isn't any sync operation yet in progress so the addProgressNotification is returning nil (which it's documented to do if the block would never be called in the case of no outstanding work). I wrapped my version of this in a main queue dispatch as suggested with the same result. (see the Realm-ProgressAPIDemo - I just added a “FirstRunDemo” app to it whose job is to keep the login screen up with a spinner until the D/L is done. The first time it runs it’ll get a few records — but not al of them even though its set it .reportIndefinitely but 4 out of 5 times it’ll just return nil and never get the notification token

AZ: [summarizing] Basically, the SyncSession silently doesn't register the notifier if the session hasn't been bound yet, and since that requires a network request and response, it seems indeterministic

propably the same issue

Yes. We have a private ticket tracking this bug as well; it will require a bit of design work to properly address.

I'm re-opening this because this is a slightly different bug that requires some support from our sync layer to properly fix. (For internal reference, this is the _private_ ticket that tracks the blocking work: https://github.com/realm/realm-sync/issues/1176.)

@austinzheng could you reword the title of this issue to more accurately reflect what this is tracking?

Actually, you're right. This is the bug caused by the issue you mentioned, and there are two other tickets tracking the related issue I mentioned above. Sorry about that, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carvalho-oak picture carvalho-oak  Â·  3Comments

TheHmmka picture TheHmmka  Â·  3Comments

dmorrow picture dmorrow  Â·  3Comments

ishidakei picture ishidakei  Â·  3Comments

menhui222 picture menhui222  Â·  3Comments