-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-35302): C++: Add new sync control methods (#3148)
## Pull Request Info Jira ticket: https://jira.mongodb.org/browse/DOCSP-35302 - [Quick Start](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-35302/sdk/cpp/quick-start/#open-a-realm-1): Regenerate the "Open a Synced Realm" example without the sync session initialization and method call. - [Sync/Manage Sync Sessions](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-35302/sdk/cpp/sync/manage-sync-session/): Add new sync control methods for C++ v1.0.0. ### 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 - **C++ SDK** - Quick Start: Remove sync session initialization and method call from the "Open a Synced Realm" example. - Sync/Manage Sync Sessions: Add new sync control methods for C++ v1.0.0. ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) --------- Co-authored-by: cbullinger <[email protected]>
- Loading branch information
1 parent
c7a44f5
commit 3c316d3
Showing
16 changed files
with
244 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <cpprealm/sdk.hpp> | ||
|
||
static const std::string APP_ID = "cpp-tester-uliix"; | ||
|
||
// :replace-start: { | ||
// "terms": { | ||
// "Local_": "", | ||
// "Sync_": "" | ||
// } | ||
// } | ||
|
||
namespace realm { | ||
struct Sync_Todo { | ||
realm::primary_key<realm::object_id> _id{realm::object_id::generate()}; | ||
std::string name; | ||
std::string status; | ||
std::string ownerId; | ||
}; | ||
REALM_SCHEMA(Sync_Todo, _id, name, status, ownerId); | ||
} // namespace realm | ||
|
||
TEST_CASE("sync session", "[realm][write][sync]") { | ||
auto appConfig = realm::App::configuration(); | ||
appConfig.app_id = APP_ID; | ||
auto app = realm::App(appConfig); | ||
auto user = app.login(realm::App::credentials::anonymous()).get(); | ||
auto syncConfig = user.flexible_sync_configuration(); | ||
auto realm = realm::db(syncConfig); | ||
// :snippet-start: sync-session | ||
auto syncSession = realm.get_sync_session(); | ||
// :snippet-end: | ||
// :snippet-start: sync-state | ||
syncSession->state(); | ||
// :snippet-end: | ||
// :snippet-start: wait-for-download | ||
syncSession->wait_for_download_completion().get(); | ||
realm.refresh(); | ||
// :snippet-end: | ||
// Remove any existing subscriptions before adding the one for this example | ||
auto clearInitialSubscriptions = | ||
realm.subscriptions().update([](auto& subs) { subs.clear(); }).get(); | ||
CHECK(clearInitialSubscriptions == true); | ||
CHECK(realm.subscriptions().size() == 0); | ||
// For this example, get the userId for the Flexible Sync query | ||
auto userId = user.identifier(); | ||
auto subscriptions = realm.subscriptions(); | ||
auto updateSubscriptionSuccess = | ||
subscriptions | ||
.update([&](realm::mutable_sync_subscription_set& subs) { | ||
subs.add<realm::Sync_Todo>("todos", [&userId](auto& obj) { | ||
// For this example, get only Sync_Todo items where the ownerId | ||
// property value is equal to the userId of the logged-in user. | ||
return obj.ownerId == userId; | ||
}); | ||
}) | ||
.get(); | ||
CHECK(updateSubscriptionSuccess == true); | ||
auto todo = realm::Sync_Todo{.name = "Create a Sync todo item", | ||
.status = "In Progress", | ||
.ownerId = userId}; | ||
|
||
realm.write([&] { realm.add(std::move(todo)); }); | ||
|
||
// :snippet-start: pause | ||
syncSession->pause(); | ||
// :snippet-end: | ||
// :snippet-start: connection-state | ||
syncSession->connection_state(); | ||
// :snippet-end: | ||
CHECK(syncSession->connection_state() == | ||
realm::internal::bridge::sync_session::connection_state::disconnected); | ||
auto todos = realm.objects<realm::Sync_Todo>(); | ||
CHECK(todos.size() == 1); | ||
auto specificTodo = todos[0]; | ||
realm.write([&] { realm.remove(specificTodo); }); | ||
// :snippet-start: resume | ||
syncSession->resume(); | ||
// :snippet-end: | ||
// :snippet-start: observe-connection-change | ||
auto connectionToken = syncSession->observe_connection_change( | ||
[&](enum realm::sync_session::connection_state, | ||
enum realm::sync_session::connection_state new_state) { | ||
// Register a block to execute when connection state changes. | ||
}); | ||
// :snippet-end: | ||
// :snippet-start: reconnect | ||
syncSession->reconnect(); | ||
// :snippet-end: | ||
// :snippet-start: unregister-observation-token | ||
syncSession->unregister_connection_change_observer(connectionToken); | ||
// :snippet-end: | ||
// :snippet-start: wait-for-upload | ||
syncSession->wait_for_upload_completion().get(); | ||
// :snippet-end: | ||
} | ||
// :replace-end: |
2 changes: 0 additions & 2 deletions
2
source/examples/generated/cpp/quick-start.snippet.open-synced-realm.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
source/examples/generated/cpp/sync-session.snippet.connection-state.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syncSession->connection_state(); |
5 changes: 5 additions & 0 deletions
5
source/examples/generated/cpp/sync-session.snippet.observe-connection-change.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
auto connectionToken = syncSession->observe_connection_change( | ||
[&](enum realm::sync_session::connection_state, | ||
enum realm::sync_session::connection_state new_state) { | ||
// Register a block to execute when connection state changes. | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syncSession->pause(); |
1 change: 1 addition & 0 deletions
1
source/examples/generated/cpp/sync-session.snippet.reconnect.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syncSession->reconnect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syncSession->resume(); |
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
source/examples/generated/cpp/sync-session.snippet.unregister-observation-token.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syncSession->unregister_connection_change_observer(connectionToken); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters