Skip to content

Commit

Permalink
(DOCSP-29399): Swift: Subscribe API documentation (#2823)
Browse files Browse the repository at this point in the history
## Pull Request Info

DO NOT MERGE until this feature ships. It depends on realm-swift PR
#8244. After release:

- [x] Update version number (`INSERT-VERSION-NUMBER-HERE`) on page with
release version
- [x] Add links to generated API documentation for new APIs
- [x] Update SPM to the release version

### Jira

- https://jira.mongodb.org/browse/DOCSP-29399

### Staged Changes

- [Manage Flexible Sync
Subscriptions](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-29399/sdk/swift/sync/flexible-sync/)

### Reminder Checklist

If your PR modifies the docs, you might need to also update some
corresponding
pages. Check if completed or N/A.

- [x] Create Jira ticket for corresponding docs-app-services update(s),
if any
- [x] Checked/updated Admin API
- [x] Checked/updated CLI reference

### Review Guidelines


[REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
  • Loading branch information
dacharyc authored Oct 6, 2023
1 parent f4cbdb4 commit 0fcf7d0
Show file tree
Hide file tree
Showing 15 changed files with 683 additions and 72 deletions.
362 changes: 333 additions & 29 deletions examples/ios/Examples/FlexibleSync.swift

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions examples/ios/Examples/RealmActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ class RealmActorTests: XCTestCase {
try await actor.removeAll()
await actor.close()
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}

#if swift(>=5.8)
func testActorIsolatedRealmAsync() async throws {
Expand Down
16 changes: 14 additions & 2 deletions examples/ios/RealmExamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 */; };
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 */; };
915B8EF929267CC100150F01 /* SwiftUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 915B8EF829267CC100150F01 /* SwiftUI.swift */; };
Expand Down Expand Up @@ -206,6 +207,7 @@
914601F526A86B9000BC91EA /* Sync.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sync.swift; sourceTree = "<group>"; };
914AC1BC28A3EAE00005E3C3 /* QuickStartFlexSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickStartFlexSync.swift; sourceTree = "<group>"; };
914E8AD727F4C08100B8591D /* ClientReset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClientReset.swift; sourceTree = "<group>"; };
9157C1BB2AD059FF0059281A /* SyncTestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncTestUtils.swift; sourceTree = "<group>"; };
915B8EE429258B4300150F01 /* CreateObjects.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateObjects.swift; sourceTree = "<group>"; };
915B8EF829267CC100150F01 /* SwiftUI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUI.swift; sourceTree = "<group>"; };
915C666927B41B0900E7BDB4 /* BundleRealms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BundleRealms.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -416,6 +418,7 @@
48DBFACD25101C3100391E2B = {
isa = PBXGroup;
children = (
9157C1BA2AD059DA0059281A /* TestUtils */,
91A09A33290AF9410009FD7E /* RealmExamplesHostApp.entitlements */,
4896EE4D2510514B00D1FABF /* Examples */,
4803B325251068F800CCAF97 /* HostApp */,
Expand Down Expand Up @@ -458,6 +461,14 @@
path = "Preview Content";
sourceTree = "<group>";
};
9157C1BA2AD059DA0059281A /* TestUtils */ = {
isa = PBXGroup;
children = (
9157C1BB2AD059FF0059281A /* SyncTestUtils.swift */,
);
path = TestUtils;
sourceTree = "<group>";
};
91713AFA28AC3D8200519F9D /* SwiftUICatalog */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -806,6 +817,7 @@
4834235C25D6E0CB002CE6B0 /* Functions.m in Sources */,
48BD8FE72580017D009DE2E6 /* OpenCloseRealm.m in Sources */,
914601F626A86B9100BC91EA /* Sync.m in Sources */,
9157C1BC2AD059FF0059281A /* SyncTestUtils.swift in Sources */,
48944DA025C0E79C0092B8AC /* EmbeddedObjects.m in Sources */,
914E8AD827F4C08100B8591D /* ClientReset.swift in Sources */,
48F38B562527926500DDEB65 /* Functions.swift in Sources */,
Expand Down Expand Up @@ -1458,8 +1470,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/realm/realm-swift.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 10.41.0;
kind = exactVersion;
version = 10.43.0;
};
};
917CA79427ECADC200F9BDDC /* XCRemoteSwiftPackageReference "facebook-ios-sdk" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
ReferencedContainer = "container:RealmExamples.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "RealmActorTests/testActorIsolatedRealmDelete()">
</Test>
<Test
Identifier = "RealmActorTests/testActorIsolatedRealmUpdate()">
</Test>
<Test
Identifier = "RealmActorTests/testObserveCollectionOnActor()">
</Test>
<Test
Identifier = "RealmActorTests/testObserveObjectOnActor()">
</Test>
<Test
Identifier = "Sync/testSetCustomLogger()">
</Test>
Expand Down
26 changes: 26 additions & 0 deletions examples/ios/TestUtils/SyncTestUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import RealmSwift
import XCTest

func randomAlphanumericString(_ length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let randomString = (0..<length).map{ _ in String(letters.randomElement()!) }.reduce("", +)
return randomString
}

open class SwiftSyncTestCase: XCTestCase {
let waiter = XCTWaiter()

public func emailPasswordCredentials(app: App) -> Credentials {
let email = "\(randomAlphanumericString(10))"
let password = "abcdef"
let credentials = Credentials.emailPassword(email: email, password: password)
let ex = XCTestExpectation(description: "Should register in the user properly")
app.emailPasswordAuth.registerUser(email: email, password: password, completion: { error in
XCTAssertNil(error)
ex.fulfill()
})
waiter.wait(for: [ex], timeout: 40)
return credentials
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ let realm = try await openFlexibleSyncRealm()
@MainActor
func openFlexibleSyncRealm() async throws -> Realm {
let app = App(id: APPID)
let user = try await app.login(credentials: Credentials.anonymous)
let credentials = emailPasswordCredentials(app: app)
let user = try await app.login(credentials: credentials)
var config = user.flexibleSyncConfiguration()
// Pass object types to the Flexible Sync configuration
// as a temporary workaround for not being able to add complete schema
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let app = App(id: APPID)

do {
let credentials = emailPasswordCredentials(app: app)
let user = try await app.login(credentials: credentials)
var flexSyncConfig = user.flexibleSyncConfiguration()
flexSyncConfig.objectTypes = [Task.self, Team.self]
do {
// Open the synced realm and manage Flexible Sync subscriptions
} catch {
print("Failed to open realm: \(error.localizedDescription)")
// handle error
}
} catch {
fatalError("Login failed: \(error.localizedDescription)")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let realm = try await Realm(configuration: flexSyncConfig)
// Add 2 subscriptions, one named and one unnamed.
let results = try await realm.objects(Team.self).where { $0.teamName == "Developer Education" }.subscribe(name: "team_developer_education")
let results2 = try await realm.objects(Task.self).where { $0.completed == false }.subscribe()
// Later, remove only the unnamed one
let subscriptions = realm.subscriptions
try await subscriptions.update {
subscriptions.removeAll(unnamedOnly: true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let realm = try await Realm(configuration: flexSyncConfig)
let results = try await realm.objects(Task.self).where { $0.completed == false }.subscribe()
// Go on to work with subscribed results.

// Later...
results.unsubscribe()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let realm = try await Realm(configuration: flexSyncConfig, actor: CustomGlobalActor.shared)
let results = try await realm.objects(Team.self)
.where { $0.teamName == "Developer Education" }
.subscribe(name: "team_developer_education")
// Go on to work with subscribed results
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let realm = try await Realm(configuration: flexSyncConfig, actor: MainActor.shared)
let results = try await realm.objects(Team.self)
.where { $0.teamName == "Developer Education" }
.subscribe(name: "team_developer_education")
// Go on to work with subscribed results
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let realm = try await Realm(configuration: flexSyncConfig)
let results = try await realm.objects(Task.self)
.where { $0.progressMinutes >= 60 }.subscribe()
// Go on to work with subscribed results
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let realm = try await Realm(configuration: flexSyncConfig)
let results = try await realm.objects(Team.self)
.where { $0.teamName == "Developer Education" }
.subscribe(name: "team_developer_education")
// Go on to work with subscribed results
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let realm = try await Realm(configuration: flexSyncConfig)
let results = try await realm.objects(Team.self)
.where { $0.members.contains("Bob Smith") }
.subscribe(
name: "bob_smith_teams",
waitForSync: .onCreation)
// After waiting for sync, the results set contains all the objects
// that match the query - in our case, 1
print("The number of teams that have Bob Smith as a member is \(results.count)")
Loading

0 comments on commit 0fcf7d0

Please sign in to comment.