Realm-js: RN: Realm JS 'write' method is handled a/synchronously?

Created on 18 Oct 2017  路  4Comments  路  Source: realm/realm-js

Goals

Tried to get data immediately from Realm DB after writing into DB using Redux via dispatching Action

Expected Results

Data on the screen update from Realm DB immediately after insert / update / delete

Actual Results

When I 'read' Realm DB and then handle previously read information it goes synchronously and next code is runned step by step.
According to documentation 'write' method takes much time so it must be called synchronously.
But when I put 'write' some data and run next some code (redux dispatch to update Store Data) it's not up to date.
Only Notifications via 'addListener' works, but those updates are often slowly even in NOT Debugger Mode.
So if 'write' is handle

Steps to Reproduce

  1. Call Realm 'write' method
  2. Call Redux Action to update Screen Data from DB via Dispatching Reducers

Code Sample

...
const {updateScreenData} = this.props;
...
realm.write(()=>{
    ...some update/insert...
});
updateScreenData(); // call Redux Action

Version of Realm and Tooling

  • Realm JS SDK Version: 1.10.3
  • React Native: 0.47
  • Client OS & Version: iOS 11.0 / iOS 10.3
  • Which debugger for React Native: Chrome Debugger / none

Is Realm JS 'write' method a/synchronously?
'Cause I can't understand those things above

Most helpful comment

Realm.write executes synchronously, so it seems entirely pointless to wrap it in a promise rather than just executing code after it returns. It may even be harmful, since you're resolving the promise within the write transaction, while you almost certainly want subsequent code to run after the transaction has been committed.

All 4 comments

wrap this in a Promise:

function write(data) {
  return new Promise(resolve => {
    realm.write(() => {
      const new = realm.create('Model', data);
      resolve(new);
    });
  })
}

And have fun:

write(data).then(new => {
  callReduxAction();
});

Even more fun with async/await:

async function doit(data) {
  const new = await write(data);
  callReduxAction();
}

Realm.write executes synchronously, so it seems entirely pointless to wrap it in a promise rather than just executing code after it returns. It may even be harmful, since you're resolving the promise within the write transaction, while you almost certainly want subsequent code to run after the transaction has been committed.

@bdash So I was plain wrong. That's good to know anyway. With async/await the subsquent statements will be run though. What doesn't make sense for me is why operations are synchronous in realm. The only asynchronous call is the Realm.open.

Resolving the promise within a write transaction will likely violate ACID principles.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matt2legit picture matt2legit  路  3Comments

kontinuity picture kontinuity  路  3Comments

MihaelIsaev picture MihaelIsaev  路  3Comments

kevinnguy picture kevinnguy  路  3Comments

emrehayirci picture emrehayirci  路  3Comments