Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/

快速入门 - C++ SDK

在此页面上

  • 导入 Realm
  • 定义对象模型
  • 打开 Realm
  • 创建、读取、更新和删除对象
  • 注意更改
  • 关闭 Realm
  • 添加 Device Sync(可选)
  • 先决条件
  • 初始化 App Services
  • 验证用户身份
  • 打开 Realm
  • 读取、写入和响应变更

本快速入门演示了如何将 Realm 与 Realm C++ SDK 结合使用。在开始之前,请确保您已安装 C++ SDK。

通过在要使用 Realm C++ SDK 的翻译单元中包含 cpprealm/sdk.hpp标头,使 Realm C++ SDK 在您的代码中可用:

#include <cpprealm/sdk.hpp>

对于仅限本地的 Realm,您可以直接在代码中定义对象模型。在本快速入门中,您可以删除ownerId ,除非您想添加可选的 Device Sync。

namespace realm {
struct Todo {
realm::primary_key<realm::object_id> _id{realm::object_id::generate()};
std::string name;
std::string status;
// The ownerId property stores the user.identifier() of a
// logged-in user. Omit this property for the non-sync example.
std::string ownerId;
};
REALM_SCHEMA(Todo, _id, name, status, ownerId);
} // namespace realm

打开 Realm 时,必须指定db_config 。您可以选择在特定路径打开 Realm,或提供sync_config来打开同步 Realm。

auto config = realm::db_config();
auto realm = realm::db(std::move(config));

有关更多信息,请参阅:配置和打开 Realm。

打开 Realm 后,您可以在写事务区块中修改它及其对象

要实例化新的 Todo 对象并将其添加到 Realm 的写入区块中,请执行以下操作:

auto todo = realm::Todo{.name = "Create my first todo item",
.status = "In Progress"};
realm.write([&] { realm.add(std::move(todo)); });

您可以检索 Realm 中所有待办事项的实时结果集合

auto todos = realm.objects<realm::Todo>();

您还可以使用以下位置过滤该集合:

auto todosInProgress = todos.where(
[](auto const& todo) { return todo.status == "In Progress"; });

要修改的待办事项,则在写事务区块中更新其属性:

auto todoToUpdate = todosInProgress[0];
realm.write([&] { todoToUpdate.status = "Complete"; });

最后,您可以删除待办事项:

realm.write([&] { realm.remove(specificTodo); });

您可以使用observe方法观察对象的更改

auto token = specificTodo.observe([&](auto&& change) {
try {
if (change.error) {
rethrow_exception(change.error);
}
if (change.is_deleted) {
std::cout << "The object was deleted.\n";
} else {
for (auto& propertyChange : change.property_changes) {
std::cout << "The object's " << propertyChange.name
<< " property has changed.\n";
}
}
} catch (std::exception const& e) {
std::cerr << "Error: " << e.what() << "\n";
}
});

要关闭 Realm 并释放所有底层资源,请调用db::close() 。关闭数据库会使所有剩余对象失效。

realm.close();

如果要跨设备同步 Realm 数据,可以设置 Atlas App Services App 并启用 Device Sync。有关 App Services 功能的详细信息,请参阅: Application Services - C++ SDK。

同步 Realm 数据之前,您必须:

要使用身份验证和同步等 App Services 功能,请使用您的App ID 访问 App Services 应用。您可在 App Services 用户界面中找到您的 App ID

auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

在此快速入门中,您将使用匿名身份验证登录用户,而不需要他们提供任何身份信息。验证用户身份后,可以为该用户打开域。

auto user = app.login(realm::App::credentials::anonymous()).get();

Realm C++ SDK 提供了许多其他方法来对用户进行身份验证、注册和链接。有关其他身份验证提供程序,请参阅:对用户进行身份验证 - C++ SDK

启用 Device Sync 并对用户进行身份验证后,您可以创建sync_configuration对象并打开 Realm。然后,您可以添加Flexible Sync 订阅,该订阅决定 Realm 可以读取和写入哪些数据。

auto syncConfig = user.flexible_sync_configuration();
auto realm = realm::db(syncConfig);
// 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::Todo>("todos", [&userId](auto& obj) {
// For this example, get only Todo items where the ownerId
// property value is equal to the userId of the logged-in user.
return obj.ownerId == userId;
});
})
.get();

在同步域上读取写入监控变更的语法与对上述非同步域的语法相同。

唯一的区别是,此示例将登录用户的user.identifier()存储在Todo项的ownerId属性中。这样,我们就可以只查询订阅中用户的待办事项,并将 Sync 权限设置为Users can only read and write their own data

有关同步权限的更多信息,请参阅基于角色的权限。

auto todo = realm::Todo{.name = "Create a Sync todo item",
.status = "In Progress",
.ownerId = userId};
realm.write([&] { realm.add(std::move(todo)); });
auto todos = realm.objects<realm::Todo>();

当您使用本地数据时,后台线程可以高效地集成、上传和下载变更集。

订阅集的每个写入事务都会产生性能成本。如果需要在会话期间对 Realm 对象进行多次更新,请考虑将编辑的对象保留在内存中,直到所有更改完成。这通过仅将完整且更新的对象写入 Realm 而不是每次更改来提高同步性能。

← 安装 C++ SDK