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

CRUD - 更新 - C++ SDK

在此页面上

  • 更新对象
  • 更新嵌入式对象属性
  • 覆盖嵌入式对象属性
  • 更新反向关系
  • 更新映射属性
  • 更新设置属性

对 Realm 对象的更新必须在写事务中进行。 有关写入事务的更多信息,请参阅:写事务(write transaction)。

您可以在写事务(write transaction)中修改 Realm 对象的属性。

// Query for the object you want to update
auto dogs = realm.objects<realm::Dog>();
auto dogsNamedMaui =
dogs.where([](auto &dog) { return dog.name == "Maui"; });
CHECK(dogsNamedMaui.size() >= 1);
// Access an object in the results set.
auto maui = dogsNamedMaui[0];
std::cout << "Dog " << maui.name.detach() << " is " << maui.age.detach()
<< " years old\n";
// Assign a new value to a member of the object in a write transaction
int64_t newAge = 2;
realm.write([&] { maui.age = newAge; });

模型

此示例使用以下模型:

struct Dog {
std::string name;
int64_t age;
};
REALM_SCHEMA(Dog, name, age)

若要更新嵌入式对象中的属性,请在写事务中修改该属性。

auto managedBusinesses = realm.objects<realm::Business>();
auto businessesNamedMongoDB = managedBusinesses.where(
[](auto &business) { return business.name == "MongoDB"; });
CHECK(businessesNamedMongoDB.size() >= 1);
auto mongoDB = businessesNamedMongoDB[0];
realm.write(
[&] { mongoDB.contactDetails->emailAddress = "info@example.com"; });
std::cout << "New email address: "
<< mongoDB.contactDetails->emailAddress.detach() << "\n";

模型

此示例使用以下模型:

namespace realm {
struct ContactDetails {
// Because ContactDetails is an embedded object, it cannot have its own _id
// It does not have a lifecycle outside of the top-level object
std::string emailAddress;
std::string phoneNumber;
};
REALM_EMBEDDED_SCHEMA(ContactDetails, emailAddress, phoneNumber)
struct Business {
realm::object_id _id;
std::string name;
ContactDetails *contactDetails;
};
REALM_SCHEMA(Business, _id, name, contactDetails)
} // namespace realm

要覆盖嵌入式对象,请在写事务(write transaction)中将嵌入式对象属性重新分配给新实例的原始指针。

auto businesses = realm.objects<realm::Business>();
auto mongoDBBusinesses = businesses.where(
[](auto &business) { return business.name == "MongoDB"; });
auto theMongoDB = mongoDBBusinesses[0];
realm.write([&] {
auto newContactDetails = realm::ContactDetails{
.emailAddress = "info@example.com", .phoneNumber = "234-567-8901"};
// Overwrite the embedded object
theMongoDB.contactDetails = &newContactDetails;
});

模型

此示例使用以下模型:

auto managedBusinesses = realm.objects<realm::Business>();
auto businessesNamedMongoDB = managedBusinesses.where(
[](auto &business) { return business.name == "MongoDB"; });
CHECK(businessesNamedMongoDB.size() >= 1);
auto mongoDB = businessesNamedMongoDB[0];
realm.write(
[&] { mongoDB.contactDetails->emailAddress = "info@example.com"; });
std::cout << "New email address: "
<< mongoDB.contactDetails->emailAddress.detach() << "\n";

您无法直接更新反向关系属性。 相反,反向关系会通过其相关的关联对象更改分配来自动更新。

在此示例中, Person对象与Dog对象具有一对一关系,而Dog Person反向关系。 当Person对象更新其Dog关系时,反向关系会自动更新。

auto config = realm::db_config();
auto realm = realm::db(std::move(config));
auto dog = realm::Dog{.name = "Wishbone"};
auto [joe] = realm.write([&realm]() {
auto person =
realm::Person{.name = "Joe", .age = 27, .dog = nullptr};
return realm.insert(std::move(person));
});
realm.write([&dog, joe = &joe]() { joe->dog = &dog; });
// After assigning `&dog` to jack's `dog` property,
// the backlink automatically updates to reflect
// the inverse relationship through the dog's `owners`
// property.
CHECK(joe.dog->owners.size() == 1);

模型

此示例使用以下模型:

struct Dog;
struct Person {
realm::primary_key<int64_t> _id;
std::string name;
int64_t age = 0;
Dog* dog;
};
REALM_SCHEMA(Person, _id, name, age, dog)
struct Dog {
realm::primary_key<int64_t> _id;
std::string name;
int64_t age = 0;
linking_objects<&Person::dog> owners;
};
REALM_SCHEMA(Dog, _id, name, age, owners)

您可以像更新标准C++映射一样更新域 映射属性

// You can check that a key exists using `find`
auto findTuesday = tommy.locationByDay.find("Tuesday");
if (findTuesday != tommy.locationByDay.end())
realm.write([&] {
tommy.locationByDay["Tuesday"] =
realm::Employee::WorkLocation::HOME;
});
;

模型

此示例使用以下模型:

namespace realm {
struct Employee {
enum class WorkLocation { HOME, OFFICE };
int64_t _id;
std::string firstName;
std::string lastName;
std::map<std::string, WorkLocation> locationByDay;
};
REALM_SCHEMA(Employee, _id, firstName, lastName, locationByDay)
} // namespace realm

您可以在写事务(write transaction)中更新设立的属性。 您可以将.insert()元素放入设立中, erase()个设立中的元素,或使用std::set算法来更改集合:

// Add elements to the set in a write transaction
realm.write([&] { managedDocsRealm.openPullRequestNumbers.insert(3066); });
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4);
// Use std::set algorithms to update a set
// In this example, use std::set_union to add elements to the set
// 3064 already exists, so it won't be added, but 3065 and 3067 are
// unique values and will be added to the set.
auto newOpenPullRequests = std::set<int64_t>({3064, 3065, 3067});
realm.write([&] {
std::set_union(
docsRealm.openPullRequestNumbers.begin(),
docsRealm.openPullRequestNumbers.end(), newOpenPullRequests.begin(),
newOpenPullRequests.end(),
std::inserter(managedDocsRealm.openPullRequestNumbers,
managedDocsRealm.openPullRequestNumbers.end()));
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 6);
// Erase elements from a set
auto it3065 = managedDocsRealm.openPullRequestNumbers.find(3065);
CHECK(it3065 != managedDocsRealm.openPullRequestNumbers.end());
realm.write([&] { managedDocsRealm.openPullRequestNumbers.erase(it3065); });

模型

此示例使用以下模型:

namespace realm {
struct Repository {
std::string ownerAndName;
std::set<int64_t> openPullRequestNumbers;
};
REALM_SCHEMA(Repository, ownerAndName, openPullRequestNumbers)
} // namespace realm

后退

读取