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

Add progress callback to asynchronous async await initialiser for Realm #8034

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion Realm/ObjectServerTests/SwiftObjectServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3061,7 +3061,6 @@ class AsyncAwaitObjectServerTests: SwiftSyncTestCase {
XCTAssertEqual(realm3.objects(SwiftHugeSyncObject.self).count, 2)
}


@MainActor func testAsyncOpenDownloadBehaviorAlways() async throws {
// Populate the Realm on the server
let user1 = try await self.app.login(credentials: basicCredentials())
Expand Down Expand Up @@ -3092,6 +3091,38 @@ class AsyncAwaitObjectServerTests: SwiftSyncTestCase {
XCTAssertEqual(realm3.objects(SwiftHugeSyncObject.self).count, 4)
}

@MainActor func testAsyncOpenWithProgressNotification() async throws {
// Populate the Realm on the server
let user1 = try await self.app.login(credentials: basicCredentials())
let realm1 = try await Realm(configuration: user1.configuration(testName: #function))
try realm1.write {
realm1.add(SwiftHugeSyncObject.create())
realm1.add(SwiftHugeSyncObject.create())
}
waitForUploads(for: realm1)

var transferred = 0
var transferrable = 0
let user2 = try await app.login(credentials: .anonymous)
var ex = expectation(description: "Progresss Notification")
let realm2 = try await Realm(configuration: user2.configuration(testName: #function),
downloadBeforeOpen: .once,
progress: { progress in
XCTAssert(progress.transferredBytes >= transferred)
XCTAssert(progress.transferrableBytes >= transferrable)
transferred = progress.transferredBytes
transferrable = progress.transferrableBytes
if progress.transferredBytes > 0 && progress.isTransferComplete {
ex.fulfill()
}
})
waitForExpectations(timeout: 10.0, handler: nil)
XCTAssert(transferred >= transferrable)

XCTAssertEqual(realm2.objects(SwiftHugeSyncObject.self).count, 2)
realm2.syncSession?.suspend()
}

func testCallResetPasswordAsyncAwait() async throws {
let email = "realm_tests_do_autoverify\(randomString(7))@\(randomString(7)).com"
let password = randomString(10)
Expand Down
10 changes: 8 additions & 2 deletions RealmSwift/Realm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,8 @@ extension Realm {
*/
@MainActor
public init(configuration: Realm.Configuration = .defaultConfiguration,
downloadBeforeOpen: OpenBehavior = .never) async throws {
downloadBeforeOpen: OpenBehavior = .never,
progress block: ((SyncSession.Progress) -> Void)? = nil) async throws {
var rlmRealm: RLMRealm?
switch downloadBeforeOpen {
case .never:
Expand All @@ -1217,13 +1218,18 @@ extension Realm {
}
case .always:
rlmRealm = try await withCheckedThrowingContinuation { continuation in
RLMRealm.asyncOpen(with: configuration.rlmConfiguration, callbackQueue: .main) { (realm, error) in
let rlmTask = RLMRealm.asyncOpen(with: configuration.rlmConfiguration, callbackQueue: .main) { (realm, error) in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dianaafanador3 thanks for the PR. Looking good.

What was your reasoning for fixing the callbackQueue to .main rather than letting an implementer pass in their preffered queue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you can only run let realm = try await Realm() at the moment, in a @mainactor.
We are still discussing if this is the bet API for this, but we have a project in progress which should give better support for actor-confined Realms and offer a better experience with Swift concurrency in general, which should include exposing recurring callbacks as AsyncSequences for sync progress notification.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great, thanks @dianaafanador3

if let error = error {
continuation.resume(with: .failure(error))
} else {
continuation.resume(with: .success(realm!))
}
}
if let block = block {
rlmTask.addProgressNotification(on: DispatchQueue.main) { transferred, transferrable in
block(SyncSession.Progress(transferred: transferred, transferrable: transferrable))
}
}
}
}
if rlmRealm == nil {
Expand Down