Skip to content

Data Loss in Nested Objects When Using Spread Operator for Update in Realm JS #6758

@tharwi

Description

@tharwi

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

class TrackingData extends Realm.Object {
	id!: number;
	progress!: number;

	static schema: Realm.ObjectSchema = {
		name: 'TrackingData',
		primaryKey: 'id',
		properties: {
			id: 'int',
			progress: 'int',
		},
	};
}

export default class Profile extends Realm.Object {
	id!: string;
	attemptID!: number;
	lastAccessDate!: Date | null;
	trackingDataList!: TrackingData[];
	sortOrder!: number;
	from!: Date;
	userId!: string;

	static schema: Realm.ObjectSchema = {
		name: 'Profile',
		primaryKey: 'id',

		properties: {
			id: 'string',
			attemptID: 'int',
			lastAccessDate: {type: 'date', optional: true},
			trackingDataList: 'TrackingData[]',
			sortOrder: 'int',
			from: 'date',
			userId: 'string',
		},
	};
}

Code Sample

const updatedProfile = { ...currentProfile, lastAccessDate: Moment.utc().format() };

realm().write(() => {
  realm().create('Profile', updatedProfile, 'modified');
});

Before Update:

{
  "id": "12345_2_2",
  "attemptID": 2,
  "lastAccessDate": null,
  "trackingDataList": [
    {
      "id": 1,
      "progress": 50
    }
  ],
  "sortOrder": 1,
  "from": "2024-06-10T10:27:02.909Z",
  "userId": "12345"
}

After Update

{
  "id": "12345_2_2",
  "attemptID": 2,
  "lastAccessDate": "2024-06-25T16:26:43Z",
  "trackingDataList": [],
  "sortOrder": 1,
  "from": "2024-06-10T10:27:02.909Z",
  "userId": "12345"
}

Stacktrace & log output

N/A

Can you reproduce the bug?

Always

Reproduction Steps

  1. Create an object in Realm with nested objects/lists.
  2. Update the object using the spread operator and change a single key.
  3. Observe that some nested lists are reset or missing after the update.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions