question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

30reactions
bdashcommented, Oct 22, 2017

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.

14reactions
ivanquirinocommented, Oct 22, 2017

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();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Realm js run query async - Stack Overflow
Realm query is synchronous, there is no way to make it async. Look like you are using realm with React Native, if this...
Read more >
How to use Realm Local DB in React Native? - Medium
In this post, I will walk you through the process of setting up a local Realm DB in your React Native application.
Read more >
Realm React Native and Node.js - MongoDB
Asynchronously submits install information to Realm. Why are we doing this? In short, because it helps us build a better product for you....
Read more >
realm | Yarn - Package Manager
This project hosts the JavaScript versions of Realm. Currently we support React Native (JSC & Hermes on iOS & Android), Node.js and Electron...
Read more >
React Native with async, await, try, catch, fetch for displaying ...
Asynchronous functions are so common that JS provides us a technique to handle them as if they were synchronous. D) Add async &...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found