2

I am inserting thousands of data into realm and found that while insertions are running, it blocks the UI and all the actions performed during this are queued and executed after insertions are completed.

Is there any way I can avoid this UI blocking or how to execute realm inserts in a separate thread(finding it difficult to do as js is single threaded).

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
Suresh Prajapati
  • 3,991
  • 5
  • 26
  • 38
  • Are you using browser based or a scripting enviroment? if browser based I'd suggest a web worker. Not sure if something comparable is available for scripting enviroments – Tschallacka Jun 21 '17 at 13:35
  • I am using it in React Native for mobile DB – Suresh Prajapati Jun 21 '17 at 13:37
  • 1
    https://github.com/devfd/react-native-workers something for you? – Tschallacka Jun 21 '17 at 13:38
  • will it insert in a different thread or same?.If it does in same thread then it will block again. – Suresh Prajapati Jun 21 '17 at 13:42
  • That's the idea of web workers, it makes a seperate thread that is non blocking to the ui. I'm only familiar with normal browser based webworkers, and not so much with react mixed in. But webworkers in themselves will run in their own non UI blocking thread, but will also not have access to the DOM, but they can perform ajax and everything else. It's intended for computation heavy/long running tasks. – Tschallacka Jun 21 '17 at 13:43
  • 1
    Ohh...... It seems that I can't access realm DB in other thread. The object is empty in the worker thread. Is it by design? or I need to do something like injecting Realm in the worker thread as we do in MainActivity.java? – Suresh Prajapati Jul 03 '17 at 07:40
  • Well, I think injecting will fail. You have to see a webworker as a pristine javascript engine, without access to DOM, but access to xhr and connections. It's meant for heavy computations. You cannot pass live objects easily along, only data. See https://stackoverflow.com/questions/19152772/how-to-pass-large-data-to-web-workers/30296330#30296330 You'll have to set up a "new" database connection in your web worker which you use for the insertions. – Tschallacka Jul 03 '17 at 07:48

0 Answers0