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

Swift: Add docs for SyncSession.reconnect() #3062

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
87 changes: 50 additions & 37 deletions examples/ios/Examples/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Sync: AnonymouslyLoggedInTestCase {
if app.currentUser != nil {
return app.currentUser!
} else {
// Instantiate the app using your Realm app ID
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// Instantiate the app using your App Services App ID
let app = App(id: APPID)
// Authenticate with the instance of the app that points
// to your backend. Here, we're using anonymous login.
let loggedInUser = try await app.login(credentials: Credentials.anonymous)
Expand All @@ -40,16 +40,12 @@ class Sync: AnonymouslyLoggedInTestCase {
func getRealm() async throws -> Realm {
// Get a logged-in app user
let user = try await getUser()
// Specify which data this authenticated user should
// be able to access.
let partitionValue = "some partition value"
// Store a configuration that consists of the current user,
// authenticated to this instance of your app, who should be
// able to access this data (partition).
var configuration = user.configuration(partitionValue: partitionValue)
// :remove-start:
configuration.objectTypes = [SyncExamples_Task.self]
// :remove-end:
// authenticated to this instance of your app,
// and what object types this database should manage.
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]

// Open a Realm with this configuration.
let realm = try await Realm(configuration: configuration)
print("Successfully opened realm: \(realm)")
Expand All @@ -58,44 +54,43 @@ class Sync: AnonymouslyLoggedInTestCase {

// Get a realm
let realm = try await getRealm()
// Do something with the realm
print("The open realm is: \(realm)")
// Add subscriptions and work with the realm
// :snippet-end:
expectation.fulfill()
wait(for: [expectation], timeout: 10)
await fulfillment(of: [expectation], timeout: 10, enforceOrder: true)
}

// :snippet-start: specify-download-behavior
func testSpecifyDownloadBehavior() async throws {
// :remove-start:
let expectation = XCTestExpectation(description: "it completes")
// :remove-end:
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let user = try await app.login(credentials: Credentials.anonymous)
let partitionValue = "some partition value"
var configuration = user.configuration(partitionValue: partitionValue)
// :remove-start:
configuration.objectTypes = [SyncExamples_Task.self]
// :remove-end:
let realm = try await Realm(configuration: configuration, downloadBeforeOpen: .always) // :emphasize:
print("Successfully opened realm after downloading: \(realm)")
// :remove-start:
// :snippet-start: specify-download-behavior
func getRealmAfterDownloadingUpdates() async throws -> Realm {
let app = App(id: APPID)
let user = try await app.login(credentials: Credentials.anonymous)
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]

let realm = try await Realm(configuration: configuration, downloadBeforeOpen: .always) // :emphasize:
print("Successfully opened realm after downloading: \(realm)")
return realm
}

let realm = try await getRealmAfterDownloadingUpdates()
print("The open realm is: \(realm)")
// Add subscription and work with the realm
// :snippet-end:
expectation.fulfill()
wait(for: [expectation], timeout: 10)
// :remove-end:
await fulfillment(of: [expectation], timeout: 10, enforceOrder: true)
}
// :snippet-end:

func testPauseResumeSyncSession() {
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// Log in...
let user = app.currentUser
let partitionValue = "some partition value"
var configuration = user!.configuration(partitionValue: partitionValue)
// :remove-start:
configuration.objectTypes = [SyncExamples_Task.self]
// :remove-end:
let syncedRealm = try! Realm(configuration: configuration)
func testPauseResumeSyncSession() async throws {
let app = App(id: APPID)
let user = try await app.login(credentials: Credentials.anonymous)
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]
let syncedRealm = try! await Realm(configuration: configuration)

// :snippet-start: pause-resume-sync-session
let syncSession = syncedRealm.syncSession!
Expand Down Expand Up @@ -138,6 +133,8 @@ class Sync: AnonymouslyLoggedInTestCase {
// :snippet-end:
}

// Leaving this example using Partition-Based Sync
// since progress notifications are currently only supported in PBS
func testCheckProgress() {
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let user = app.currentUser
Expand Down Expand Up @@ -223,5 +220,21 @@ class Sync: AnonymouslyLoggedInTestCase {
}
// :snippet-end:
}

func testSyncSessionReconnect() async throws {
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let user = try await app.login(credentials: Credentials.anonymous)
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]
let realm = try! await Realm(configuration: configuration)

// :snippet-start: sync-session-reconnect
let syncSession = realm.syncSession!

// Work with the realm. When you need to force the sync session to reconnect...
syncSession.reconnect()
// :snippet-end:

}
}
// :replace-end:
2 changes: 1 addition & 1 deletion examples/ios/RealmExamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@
repositoryURL = "https://github.com/realm/realm-swift.git";
requirement = {
kind = exactVersion;
version = 10.43.0;
version = 10.44.0;
};
};
917CA79427ECADC200F9BDDC /* XCRemoteSwiftPackageReference "facebook-ios-sdk" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ func getUser() async throws -> User {
if app.currentUser != nil {
return app.currentUser!
} else {
// Instantiate the app using your Realm app ID
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// Instantiate the app using your App Services App ID
let app = App(id: APPID)
// Authenticate with the instance of the app that points
// to your backend. Here, we're using anonymous login.
let loggedInUser = try await app.login(credentials: Credentials.anonymous)
Expand All @@ -19,13 +19,12 @@ func getUser() async throws -> User {
func getRealm() async throws -> Realm {
// Get a logged-in app user
let user = try await getUser()
// Specify which data this authenticated user should
// be able to access.
let partitionValue = "some partition value"
// Store a configuration that consists of the current user,
// authenticated to this instance of your app, who should be
// able to access this data (partition).
var configuration = user.configuration(partitionValue: partitionValue)
// authenticated to this instance of your app,
// and what object types this database should manage.
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]

// Open a Realm with this configuration.
let realm = try await Realm(configuration: configuration)
print("Successfully opened realm: \(realm)")
Expand All @@ -34,5 +33,5 @@ func getRealm() async throws -> Realm {

// Get a realm
let realm = try await getRealm()
// Do something with the realm
print("The open realm is: \(realm)")
// Add subscriptions and work with the realm
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
.. code-block:: swift
:emphasize-lines: 6
:emphasize-lines: 7

func testSpecifyDownloadBehavior() async throws {
let app = App(id: YOUR_REALM_APP_ID)
func getRealmAfterDownloadingUpdates() async throws -> Realm {
let app = App(id: APPID)
let user = try await app.login(credentials: Credentials.anonymous)
let partitionValue = "some partition value"
var configuration = user.configuration(partitionValue: partitionValue)
var configuration = user.flexibleSyncConfiguration()
configuration.objectTypes = [FlexibleSync_Task.self, FlexibleSync_Team.self]

let realm = try await Realm(configuration: configuration, downloadBeforeOpen: .always)
print("Successfully opened realm after downloading: \(realm)")
return realm
}

let realm = try await getRealmAfterDownloadingUpdates()
print("The open realm is: \(realm)")
// Add subscription and work with the realm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let syncSession = realm.syncSession!

// Work with the realm. When you need to force the sync session to reconnect...
syncSession.reconnect()
32 changes: 32 additions & 0 deletions source/sdk/swift/sync/sync-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,35 @@ Check Upload & Download Progress for a Sync Session

.. literalinclude:: /examples/generated/code/start/Sync.snippet.check-progress.m
:language: objectivec

.. _ios-reconnect-sync-sessions:

Manually Reconnect All Sync Sessions
------------------------------------

.. versionadded:: 10.44.0

Realm automatically detects when a device regains connectivity after being
offline and attempts to reconnect using an incremental backoff strategy.

In Swift SDK version 10.44.0 and later, you can choose to manually trigger a
reconnect attempt with ``SyncSession.reconnect()``
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to link to the API ref here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Swift doesn't have an API ref for this method, it seems - only Objective-C. I meant to ask about this in the tech review - I'll add a note to my PR description about it 👍

instead of waiting for the duration of the incremental backoff. This is
useful if you have a more accurate understanding of
the network conditions and don't want to rely on Realm's automatic
reconnect detection. The SDK also automatically calls this method when a device
toggles off airplane mode.

.. literalinclude:: /examples/generated/code/start/Sync.snippet.sync-session-reconnect.swift
:language: swift

When you call this method, the SDK forces all sync sessions to attempt to
reconnect immediately and resets any timers used for incremental
backoff.

.. important:: Cannot Reconnect Within Socket Read Timeout Duration

Realm has an internal default socket read timeout of 2 minutes, where
Realm will time out if a read operation does not receive any data
within a 2-minute window. If you call ``SyncSession.reconnect()``
within that window, the Swift SDK does *not* attempt to reconnect.
Loading