Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow addProgressNotification when using async initializer #7763

Open
rowungiles opened this issue Apr 22, 2022 · 4 comments
Open

Allow addProgressNotification when using async initializer #7763

rowungiles opened this issue Apr 22, 2022 · 4 comments

Comments

@rowungiles
Copy link

rowungiles commented Apr 22, 2022

Problem

When using let realm = try await Realm(configuration: configuration, downloadBeforeOpen: .once) I'd like to track the download progress of that sync.

Looking at the async initializer that isn't possible as task is hidden (as is callbackQueue):

rlmRealm = try await withCheckedThrowingContinuation { continuation in
                RLMRealm.asyncOpen(with: configuration.rlmConfiguration, callbackQueue: .main) { (realm, error) in
                    if let error = error {
                        continuation.resume(with: .failure(error))
                    } else {
                        continuation.resume(with: .success(realm!))
                    }
                }
            }

Solution

With a goal of minimising design changes, the smallest change I can think of is passing in a block and a queue to set internally on the task returned from RLMRealm.asyncOpen.

@MainActor
    public init(configuration: Realm.Configuration = .defaultConfiguration,
                downloadBeforeOpen: OpenBehavior = .never,
                callbackQueue: DispatchQueue = .main,
                block: @escaping (SyncSession.Progress) -> Void = nil) async throws {
        var rlmRealm: RLMRealm?
        switch downloadBeforeOpen {
        case .never:
            break
        case .once:
            if !Realm.fileExists(for: configuration) {
                fallthrough
            }
        case .always:
            rlmRealm = try await withCheckedThrowingContinuation { continuation in
                let task = RLMRealm.asyncOpen(with: configuration.rlmConfiguration, callbackQueue: callbackQueue) { (realm, error) in
                    if let error = error {
                        continuation.resume(with: .failure(error))
                    } else {
                        continuation.resume(with: .success(realm!))
                    }
                }
                task.addProgressNotification(queue: callbackQueue) { progress in
                        block(progress)
                }
            }
        }
        if rlmRealm == nil {
            rlmRealm = try RLMRealm(configuration: configuration.rlmConfiguration)
        }
        self.init(rlmRealm!)
    }

How important is this improvement for you?

Would be a major improvement

@tcollins590
Copy link

I'm running into the same issue. I'm about to move away from Async/Await for opening the realm until I can track downloads.

@dianaafanador3
Copy link
Collaborator

dianaafanador3 commented Nov 16, 2022

Hi @captn590 @rowungiles Thanks for reaching. We are taking a look at this and discussing this with the team, so we can come up with the best solution (API) for this.

@bdkjones
Copy link

Was there ever a resolution here, or is this another one of those, "we'll get back to you in 7 years to tell you we still don't have an answer" issues?

AsyncOpen can take a long time to "bootstrap changesets" and not being able to show progress to the user is very frustrating.

Please fill this hole--enough talking; get the feature shipped!

@nirinchev
Copy link
Member

That is fair criticism. I'll see if we can add it as part of the project that adds support for bootstrap progress notifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants