realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.8k stars 576 forks source link

Help: set property values out a transaction #951

Closed red3 closed 7 years ago

red3 commented 7 years ago

I am trying to do follows:

var records = realm.objects('AppRecord')
if (records.length > 0) {
  var r = records[0];
  r.icon = 'host/xxx.png';
  console.log(r);
}

However, I got this error: Error: Can only set property values within a transaction.

I know this is for auto-updating feature, but I still wonder is there a way to modify a property out a transaction, cause in my business, I need to add a host string to the icon property and then return to restfull API service.

red3 commented 7 years ago

Currently, I am using this:

  JSON.parse(JSON.stringify(r));

but I am worrying about its efficiency.

blagoev commented 7 years ago

Can you use Object.assign() https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

red3 commented 7 years ago

It seemed that Object.assign() is more efficient without deep clone.