-
Notifications
You must be signed in to change notification settings - Fork 575
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
Data Loss in Nested Objects When Using Spread Operator for Update in Realm JS #6758
Comments
➤ PM Bot commented: Jira ticket: RJS-2847 |
Hi @kneth, thanks for the reply. Can you change the below line
to this
|
@tharwi, the issue you're seeing is more related to what you're assigning rather than the spread operation itself. When you're using the spread operator it performs a shallow copy, so the This means that when you pass that This is of course something we'd like to fix as soon as possible and the initial work can be tracked here. As an example, let's say you want to update the class ListItem extends Realm.Object {
value!: number;
static schema: ObjectSchema = {
name: "ListItem",
properties: {
value: "int",
},
};
}
class ObjectWithList extends Realm.Object {
_id!: BSON.ObjectId;
list!: Realm.List<ListItem>;
valueToUpdate!: string;
static schema: ObjectSchema = {
name: "ObjectWithList",
primaryKey: "_id",
properties: {
_id: "objectId",
list: "ListItem[]",
valueToUpdate: "string",
},
};
}
const realm = new Realm({ schema: [ObjectWithList, ListItem] });
const _id = new BSON.ObjectId();
const object = realm.write(() => {
return realm.create(ObjectWithList, { _id, list: [{ value: 1 }], valueToUpdate: "original" });
});
expect(object.list.length).equals(1);
const objectShallowCopy = { ...object };
// Since it's a shallow copy, the list is still the Realm List.
expect(objectShallowCopy.list).to.be.instanceOf(Realm.List);
realm.write(() => {
// Unfortunately, passing in the same Realm List again basically
// performs a self-assignment, clearing the list.
return realm.create(ObjectWithList, objectShallowCopy, UpdateMode.Modified);
});
// 💥 This will fail.
expect(object.list.length).equals(1); Workaround: From the code example you provided, it looks like you only want to update your realm.write(() => {
- return realm.create(ObjectWithList, objectShallowCopy, UpdateMode.Modified);
+ return realm.create(ObjectWithList, { _id, valueToUpdate: "updated" }, UpdateMode.Modified);
}); Or skip realm.write(() => {
- return realm.create(ObjectWithList, objectShallowCopy, UpdateMode.Modified);
+ object.valueToUpdate = "updated";
}); |
Hi @elle-j , Thanks for the update. Will use one of the workaround for now. |
Closing since this is tracked in realm/realm-core#7422. |
How frequently does the bug occur?
Always
Description
I am experiencing data loss in nested objects when updating an object in Realm JS using the spread operator. Specifically, after the update, one of my nested lists (trackingDataList) gets reset to an empty list.
Realm models
Code Sample
Before Update:
After Update
Stacktrace & log output
Can you reproduce the bug?
Always
Reproduction Steps
Version
12.10.0
What services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
iOS 17.2
Build environment
react-native: 0.74.2
node: v18.17.1
Cocoapods version
1.15.2
The text was updated successfully, but these errors were encountered: