Skip to content

Commit

Permalink
(DOCSP-33996): Swift: Update progress notification documentation (mon…
Browse files Browse the repository at this point in the history
…godb#3244)

## Pull Request Info

Jira ticket: https://jira.mongodb.org/browse/DOCSP-33996

- [Manage Sync
Sessions](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-33996/sdk/swift/sync/sync-session/#check-upload---download-progress-for-a-sync-session):
Add a text description with some details. Update code examples.

### Reminder Checklist

Before merging your PR, make sure to check a few things.

- [x] Did you tag pages appropriately?
  - genre
  - meta.keywords
  - meta.description
- [x] Describe your PR's changes in the Release Notes section
- [x] Create a Jira ticket for related docs-app-services work, if any

### Release Notes

- **Swift SDK**
- Sync Data/Manage Sync Sessions: Update the documentation for Upload
and Download Progress Notifications, which have been updated for
compatibility with Flexible Sync.

### Review Guidelines


[REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)

---------

Co-authored-by: cbullinger <115956901+cbullinger@users.noreply.github.com>
  • Loading branch information
dacharyc and cbullinger authored May 6, 2024
1 parent 475864c commit ec69835
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 110 deletions.
14 changes: 14 additions & 0 deletions examples/ios/Examples/CustomUserData.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ - (void)testUpdateCustomUserData {
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"];
RLMMongoDatabase *database = [client databaseWithName:@"my_database"];
RLMMongoCollection *collection = [database collectionWithName:@"users"];
// :remove-start:
[collection insertOneDocument:
@{@"userId": [user identifier], @"favoriteColor": @"pink"}
completion:^(id<RLMBSON> newObjectId, NSError *error) {
if (error != nil) {
NSLog(@"Failed to insert: %@", error);
}
NSLog(@"Inserted custom user data document with object ID: %@", newObjectId);
// :remove-start:
XCTAssertNotNil(newObjectId);
// :remove-end:
}];
sleep(5);
// :remove-end:

// Update the user's custom data document
[collection updateOneDocumentWhere:@{@"userId": [user identifier]}
Expand Down
15 changes: 15 additions & 0 deletions examples/ios/Examples/CustomUserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ class CustomUserData: XCTestCase {
let client = user.mongoClient("mongodb-atlas")
let database = client.database(named: "my_database")
let collection = database.collection(withName: "users")
// :remove-start:
collection.insertOne([
"userId": AnyBSON(user.id),
"favoriteColor": "pink"
]) { (result) in
switch result {
case .failure(let error):
print("Failed to insert document: \(error.localizedDescription)")
case .success(let newObjectId):
print("Inserted custom user data document with object ID: \(newObjectId)")
XCTAssertNotNil(newObjectId)
}
}
sleep(5)
// :remove-end:
collection.updateOneDocument(
filter: ["userId": AnyBSON(user.id)],
update: ["favoriteColor": "cerulean"]
Expand Down
7 changes: 7 additions & 0 deletions examples/ios/Examples/DeleteUsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ class DeleteUsers: XCTestCase {
XCTAssertEqual(app.allUsers.count, 0)
}
// :snippet-end:

override func setUp() async throws {
for user in app.allUsers {
try await user.value.delete()
}
XCTAssertEqual(app.allUsers.count, 0)
}
}
6 changes: 3 additions & 3 deletions examples/ios/Examples/Sync.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ - (void)testCheckProgress {
// :snippet-start: check-progress
RLMSyncSession *syncSession = [syncedRealm syncSession];
RLMProgressNotificationToken *token = [syncSession
addProgressNotificationForDirection:RLMSyncProgressDirectionUpload
addSyncProgressNotificationForDirection:RLMSyncProgressDirectionUpload
mode:RLMSyncProgressModeForCurrentlyOutstandingWork
block:^(NSUInteger transferredBytes, NSUInteger transferrableBytes) {
NSLog(@"Uploaded %luB / %luB", (unsigned long)transferredBytes, transferrableBytes);
block:^(RLMSyncProgress syncProgress) {
NSLog(@"Uploaded %fB", (double)syncProgress.progressEstimate);
// :remove-start:
[expectation fulfill];
// :remove-end:
Expand Down
64 changes: 56 additions & 8 deletions examples/ios/Examples/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,77 @@ class Sync: AnonymouslyLoggedInTestCase {
let expectation = XCTestExpectation(description: "it completes")
expectation.assertForOverFulfill = false

// :snippet-start: check-progress
let syncSession = syncedRealm.syncSession!
let token = syncSession.addProgressNotification(
for: .upload, mode: .forCurrentlyOutstandingWork) { (progress) in

let transferredBytes = progress.transferredBytes
let transferrableBytes = progress.transferrableBytes
let transferPercent = progress.fractionTransferred * 100
let transferPercent = progress.progressEstimate * 100

print("Uploaded \(transferredBytes)B / \(transferrableBytes)B (\(transferPercent)%)")
// :remove-start:
print("Uploaded (\(transferPercent)%)")
expectation.fulfill()
// :remove-end:
}

// Upload something
try! syncedRealm.write {
syncedRealm.add(SyncExamples_Task())
}
// :snippet-end:
wait(for: [expectation], timeout: 10)
}

@MainActor
func testCheckProgressFlexibleSync() async {
let app = App(id: APPID)
let expectation = XCTestExpectation(description: "Progress notification is sent")

do {
let user = try await app.login(credentials: Credentials.anonymous)
var flexSyncConfig = user.flexibleSyncConfiguration()
flexSyncConfig.objectTypes = [FlexibleSync_Task.self]
do {
let realm = try await Realm(configuration: flexSyncConfig)
let subscriptions = realm.subscriptions
try await subscriptions.update {
subscriptions.append(QuerySubscription<FlexibleSync_Task>())
}
checkSyncProgress(realm: realm)
} catch {
print("Failed to open realm: \(error.localizedDescription)")
// handle error
}
} catch {
fatalError("Login failed: \(error.localizedDescription)")
}

func checkSyncProgress(realm: Realm) {
let todo = FlexibleSync_Task()
todo.dueDate = (Date.now + TimeInterval(86400))
todo.taskName = "This task should appear in the Flex Sync realm"
todo.progressMinutes = 20
todo.completed = false

// :snippet-start: check-progress-estimate
let syncSession = realm.syncSession!
let token = syncSession.addProgressNotification(
for: .upload, mode: .forCurrentlyOutstandingWork) { (progress) in

let progressEstimate = progress.progressEstimate
let transferPercent = progressEstimate * 100

print("Uploaded (\(transferPercent)%)")
// :remove-start:
// Verify that progress increases.
XCTAssertGreaterThanOrEqual(progress.progressEstimate, progressEstimate)
expectation.fulfill()
// :remove-end:
}
// :snippet-end:
try! realm.write {
realm.add(todo)
}
}
await fulfillment(of: [expectation], timeout: 10)
}

func testSetClientLogLevelDeprecated() {
// :snippet-start: set-log-level-deprecated
// This code example shows how to set the log level
Expand Down
82 changes: 8 additions & 74 deletions examples/ios/RealmExamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
914601F726A86B9100BC91EA /* Sync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 914601F526A86B9000BC91EA /* Sync.swift */; };
914AC1BD28A3EAE00005E3C3 /* QuickStartFlexSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 914AC1BC28A3EAE00005E3C3 /* QuickStartFlexSync.swift */; };
914E8AD827F4C08100B8591D /* ClientReset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 914E8AD727F4C08100B8591D /* ClientReset.swift */; };
91508B792BE57F3F00817DBC /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 91508B782BE57F3F00817DBC /* RealmSwift */; };
9157C1BC2AD059FF0059281A /* SyncTestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9157C1BB2AD059FF0059281A /* SyncTestUtils.swift */; };
915B8EE529258B4300150F01 /* CreateObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 915B8EE429258B4300150F01 /* CreateObjects.swift */; };
915B8EE629258B4300150F01 /* CreateObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 915B8EE429258B4300150F01 /* CreateObjects.swift */; };
Expand All @@ -84,21 +85,12 @@
91713B1728AC3D8400519F9D /* SwiftUICatalogUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B1628AC3D8400519F9D /* SwiftUICatalogUITests.swift */; };
91713B2528AC3DF300519F9D /* OpenRealm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B2428AC3DF300519F9D /* OpenRealm.swift */; };
91713B2728AC3DF300519F9D /* OpenRealm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B2428AC3DF300519F9D /* OpenRealm.swift */; };
91713B2928AC3E7A00519F9D /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 91713B2828AC3E7A00519F9D /* Realm */; };
91713B2B28AC3E7A00519F9D /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 91713B2A28AC3E7A00519F9D /* RealmSwift */; };
91713B2D28AC400400519F9D /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 91713B2C28AC400400519F9D /* Realm */; };
91713B2F28AC400400519F9D /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 91713B2E28AC400400519F9D /* RealmSwift */; };
91713B3128AC41B700519F9D /* Authenticate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3028AC41B700519F9D /* Authenticate.swift */; };
91713B3328AC41B700519F9D /* Authenticate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3028AC41B700519F9D /* Authenticate.swift */; };
91713B3528AD2B0E00519F9D /* PassObjectsToView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3428AD2B0E00519F9D /* PassObjectsToView.swift */; };
91713B3728AD2B0E00519F9D /* PassObjectsToView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3428AD2B0E00519F9D /* PassObjectsToView.swift */; };
91713B3928AD2B3500519F9D /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3828AD2B3500519F9D /* Model.swift */; };
91713B3B28AD2B3500519F9D /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91713B3828AD2B3500519F9D /* Model.swift */; };
9175294828467FA000F8CB03 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 9175294728467FA000F8CB03 /* RealmSwift */; };
9175294A28467FB700F8CB03 /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 9175294928467FB700F8CB03 /* Realm */; };
9175294C28467FB700F8CB03 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 9175294B28467FB700F8CB03 /* RealmSwift */; };
917529522846803200F8CB03 /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 917529512846803200F8CB03 /* Realm */; };
917529542846803200F8CB03 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 917529532846803200F8CB03 /* RealmSwift */; };
91787E5429E594C100296021 /* RealmActor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91787E5329E594C100296021 /* RealmActor.swift */; };
917C6E662A8E638F00EC3079 /* SupportedTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 917C6E652A8E638F00EC3079 /* SupportedTypes.swift */; };
917C6E682A8E63E900EC3079 /* SupportedTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 917C6E672A8E63E900EC3079 /* SupportedTypes.m */; };
Expand Down Expand Up @@ -126,7 +118,6 @@
91E5C80128256C6D00AAA239 /* EventLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91E5C80028256C6D00AAA239 /* EventLibrary.swift */; };
91E608AD296E114500F84EA5 /* SyncedRealmCRUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91E608AC296E114500F84EA5 /* SyncedRealmCRUD.swift */; };
91F046992A29299A000B43B2 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91F046982A29299A000B43B2 /* Logging.swift */; };
91F04E9A2BE290A80082EB2B /* RealmSwift in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 9175294728467FA000F8CB03 /* RealmSwift */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
91FBD320279865080005C10C /* DeleteUsers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91FBD31F279865080005C10C /* DeleteUsers.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -154,7 +145,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
91F04E9A2BE290A80082EB2B /* RealmSwift in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -285,7 +275,7 @@
files = (
917CA79827ECADC200F9BDDC /* FacebookLogin in Frameworks */,
9143E95327EB79BD0082A5D6 /* GoogleSignIn in Frameworks */,
9175294828467FA000F8CB03 /* RealmSwift in Frameworks */,
91508B792BE57F3F00817DBC /* RealmSwift in Frameworks */,
917CA79627ECADC200F9BDDC /* FacebookCore in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -294,35 +284,27 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9175294C28467FB700F8CB03 /* RealmSwift in Frameworks */,
9175294A28467FB700F8CB03 /* Realm in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
91713AF628AC3D8200519F9D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
91713B2B28AC3E7A00519F9D /* RealmSwift in Frameworks */,
91713B2928AC3E7A00519F9D /* Realm in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
91713B0F28AC3D8300519F9D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
91713B2F28AC400400519F9D /* RealmSwift in Frameworks */,
91713B2D28AC400400519F9D /* Realm in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
91C687FA274D8AFE001A5DBE /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
917529542846803200F8CB03 /* RealmSwift in Frameworks */,
917529522846803200F8CB03 /* Realm in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -580,7 +562,7 @@
9143E95227EB79BD0082A5D6 /* GoogleSignIn */,
917CA79527ECADC200F9BDDC /* FacebookCore */,
917CA79727ECADC200F9BDDC /* FacebookLogin */,
9175294728467FA000F8CB03 /* RealmSwift */,
91508B782BE57F3F00817DBC /* RealmSwift */,
);
productName = RealmExampleTests;
productReference = 4896EE4C2510514B00D1FABF /* RealmExamples.xctest */;
Expand All @@ -600,8 +582,6 @@
);
name = SwiftUIFlexSyncExample;
packageProductDependencies = (
9175294928467FB700F8CB03 /* Realm */,
9175294B28467FB700F8CB03 /* RealmSwift */,
);
productName = SwiftUIFlexSyncExample;
productReference = 912ACA7828415292006CDDD8 /* SwiftUIFlexSyncExample.app */;
Expand All @@ -621,8 +601,6 @@
);
name = SwiftUICatalog;
packageProductDependencies = (
91713B2828AC3E7A00519F9D /* Realm */,
91713B2A28AC3E7A00519F9D /* RealmSwift */,
);
productName = SwiftUICatalog;
productReference = 91713AF928AC3D8200519F9D /* SwiftUICatalog.app */;
Expand All @@ -643,8 +621,6 @@
);
name = SwiftUICatalogUITests;
packageProductDependencies = (
91713B2C28AC400400519F9D /* Realm */,
91713B2E28AC400400519F9D /* RealmSwift */,
);
productName = SwiftUICatalogUITests;
productReference = 91713B1228AC3D8300519F9D /* SwiftUICatalogUITests.xctest */;
Expand All @@ -664,8 +640,6 @@
);
name = SwiftUIExamples;
packageProductDependencies = (
917529512846803200F8CB03 /* Realm */,
917529532846803200F8CB03 /* RealmSwift */,
);
productName = QuickStartSwiftUI;
productReference = 91C68804274D8AFE001A5DBE /* SwiftUIExamples.app */;
Expand Down Expand Up @@ -713,7 +687,7 @@
packageReferences = (
9143E95127EB79BD0082A5D6 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */,
917CA79427ECADC200F9BDDC /* XCRemoteSwiftPackageReference "facebook-ios-sdk" */,
9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */,
91508B752BE57EFA00817DBC /* XCRemoteSwiftPackageReference "realm-swift" */,
);
productRefGroup = 48DBFAD725101C3100391E2B /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1485,12 +1459,12 @@
version = 6.1.0;
};
};
9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */ = {
91508B752BE57EFA00817DBC /* XCRemoteSwiftPackageReference "realm-swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/realm/realm-swift.git";
requirement = {
kind = exactVersion;
version = 10.49.3;
version = 10.50.0;
};
};
917CA79427ECADC200F9BDDC /* XCRemoteSwiftPackageReference "facebook-ios-sdk" */ = {
Expand All @@ -1509,49 +1483,9 @@
package = 9143E95127EB79BD0082A5D6 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
productName = GoogleSignIn;
};
91713B2828AC3E7A00519F9D /* Realm */ = {
91508B782BE57F3F00817DBC /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = Realm;
};
91713B2A28AC3E7A00519F9D /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = RealmSwift;
};
91713B2C28AC400400519F9D /* Realm */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = Realm;
};
91713B2E28AC400400519F9D /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = RealmSwift;
};
9175294728467FA000F8CB03 /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = RealmSwift;
};
9175294928467FB700F8CB03 /* Realm */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = Realm;
};
9175294B28467FB700F8CB03 /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = RealmSwift;
};
917529512846803200F8CB03 /* Realm */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = Realm;
};
917529532846803200F8CB03 /* RealmSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9175294428467FA000F8CB03 /* XCRemoteSwiftPackageReference "realm-swift" */;
package = 91508B752BE57EFA00817DBC /* XCRemoteSwiftPackageReference "realm-swift" */;
productName = RealmSwift;
};
917CA79527ECADC200F9BDDC /* FacebookCore */ = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let syncSession = realm.syncSession!
let token = syncSession.addProgressNotification(
for: .upload, mode: .forCurrentlyOutstandingWork) { (progress) in

let progressEstimate = progress.progressEstimate
let transferPercent = progressEstimate * 100

print("Uploaded (\(transferPercent)%)")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
RLMSyncSession *syncSession = [syncedRealm syncSession];
RLMProgressNotificationToken *token = [syncSession
addProgressNotificationForDirection:RLMSyncProgressDirectionUpload
addSyncProgressNotificationForDirection:RLMSyncProgressDirectionUpload
mode:RLMSyncProgressModeForCurrentlyOutstandingWork
block:^(NSUInteger transferredBytes, NSUInteger transferrableBytes) {
NSLog(@"Uploaded %luB / %luB", (unsigned long)transferredBytes, transferrableBytes);
block:^(RLMSyncProgress syncProgress) {
NSLog(@"Uploaded %fB", (double)syncProgress.progressEstimate);
}];

// Upload something
Expand Down
Loading

0 comments on commit ec69835

Please sign in to comment.