Comments (6)
➤ PM Bot commented:
Jira ticket: RJS-2847
from realm-js.
@tharwi I am not able to reproduce. How does your code differs from #6774?
from realm-js.
Hi @kneth, thanks for the reply.
Can you change the below line
const updatedProfile = { ...currentProfile, lastAccessDate: new Date() };
to this
const profile = realmInstance.objects('Profile')[0];
const updatedProfile = {...profile, lastAccessDate: new Date()};
from realm-js.
@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 trackingDataList
on your object will still be the Realm.List
from your currentProfile
.
This means that when you pass that Realm.List
to realm.create(.., .., UpdateMode.Modified)
, it will basically perform a self-assignment (e.g. object.list = object.list
). We do not cache our collections, so even though you're using UpdateMode.Modified
, we currently do not know if it's actually the same list being assigned in this case, and we need to clear the underlying collection being assigned to before adding the items of the RHS list (which is why the list becomes empty here).
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 valueToUpdate
property below:
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 lastAccessDate
field. You can instead pass only the fields to be updated to realm.create()
when using UpdateMode.Modified
, rather than passing the spread.
realm.write(() => {
- return realm.create(ObjectWithList, objectShallowCopy, UpdateMode.Modified);
+ return realm.create(ObjectWithList, { _id, valueToUpdate: "updated" }, UpdateMode.Modified);
});
Or skip realm.create()
and update the field on the object directly:
realm.write(() => {
- return realm.create(ObjectWithList, objectShallowCopy, UpdateMode.Modified);
+ object.valueToUpdate = "updated";
});
from realm-js.
Hi @elle-j , Thanks for the update. Will use one of the workaround for now.
from realm-js.
Closing since this is tracked in realm/realm-core#7422.
from realm-js.
Related Issues (20)
- Error: Realm at path *** already opened with a different schema mode in React Native HOT 4
- Package path . is not exported from package HOT 5
- 'object type 'f' not found in schema' occurs when updating the application via code push appcenter HOT 4
- Remapped Property Key Not Working with Sorting HOT 1
- Keep login information persistent with 12.0.0-browser.2 HOT 3
- toJSON() serialization of BSON.ObjectId to String HOT 1
- `@realm/react`: Passing an app as a prop to `AppProvider` HOT 1
- Error: Database has an unsupported version (24) and cannot be upgraded HOT 2
- Gradle lint error with 12.10.0 and yarn errors with 12.11.1 HOT 4
- Unable to resolve "../../../realm/lib/browser$1" from "node_modules/@realm/react/dist/index.cjs" HOT 3
- Sync protocol version issue in 12.0.0-browser.2 HOT 3
- Add `useProgress` to `@realm/react` HOT 1
- Add `useProgress` to `@realm/react` HOT 4
- Improve Electron support by allowing renderer process "context isolation" and disabled "node integration" HOT 9
- Could not find realm binary when show react-native component in native iOS app HOT 1
- Realm does not build on iOS with `use_frameworks! :linkage => :static` as of version 12.12.0 HOT 11
- Add relaxed schema extensions to realm objects via dynamic API HOT 1
- Can't use Realm with new react native architecture HOT 2
- Expo EAS build started failing with command not found: cmake-js HOT 3
- Stabalize `OpenBehaviour` tests HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from realm-js.