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)
- Improve the experience of using setters & getters with embedded objects HOT 7
- Rendering RealmProvider inside Expo's <Slot/> causes infinite loop HOT 8
- Application crash when using Realm logger with 'all' log level HOT 3
- Erorr while trying build react native android app HOT 1
- Data Seeding Performance in realm-js 12 vs 11 is nearly 10x slow HOT 4
- [Violation] 'message' handler took 215ms HOT 2
- The Realm is already in a write transaction HOT 7
- Can't build app without new architecture and bridgeless mode on react native 0.74.4 and 0.74.5 HOT 2
- Fatal Exception: RCTFatalException: Unhandled JS Exception: Error: Failed to open file at path '/var/mobile/Containers/Data/Application/2FE5E895-9E44-4FB7-9757-DBF3EE4362D7/Library/app_internal_data//default.realm.lock': Operation not permitted, js engine: hermes HOT 1
- Reenable OpenBehavior tests HOT 1
- Getting error: Another authentication operation is already in progress, when installing new app after upgrading to 12.12.1 HOT 1
- Upgrading @realm/react from 0.7.0 to 0.8.0+ breaks app HOT 6
- > Task :realm:buildCMakeDebug[arm64-v8a][realm-js-android-binding] FAILED . React native - 0.75.1 HOT 9
- [iOS] No matching member function for call to 'invokeAsync' [React Native Version --> 0.75.1] HOT 10
- What went wrong: Execution failed for task ':realm:configureCMakeDebug[arm64-v8a]'. HOT 9
- Failed to open Realm file at path '/data/data/<our_package>/files/default.realm': Failed to memory buffer:Invalid top array size (ref: 5104, array size: 0) file size: 0, read lock size: none, read lock version: none HOT 1
- Upgrade to 12.13.0 with yarn throw a memory error HOT 2
- [TypeError: _$$_REQUIRE(_dependencyMap[6], "realm").Object.assign is not a function (it is undefined)] HOT 2
- react sync problems with 12.0.0-browser.2 HOT 3
- useObject: causing re-render continously HOT 2
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.