tezvyn:

Threading of native module methods

AI-drafted, machine-checkedintermediate
WHAT IT TESTS

native module threading.

OUTLINE

methods run on a dedicated native module queue, not the UI thread; long blocking work stalls other module calls; offload to a background executor or override methodQueue, returning results via promise.

WHAT THIS TESTS The interviewer probes your understanding of where native module code runs and how to avoid blocking, which is essential for keeping both the UI and JS responsive.

A GOOD ANSWER COVERS On the legacy architecture, native module methods do not run on the JavaScript thread and, importantly, not on the main UI thread by default either; React Native dispatches them on a dedicated native modules thread or a per-module GCD queue. On iOS each module gets a methodQueue, by default a shared background queue, and you can override methodQueue to control it. On Android methods run on the native modules thread. The problem arises when a method does a long synchronous task, such as parsing a huge file, doing heavy crypto, or a slow database scan, directly on that queue: it serializes work and delays other native module calls, and if you mistakenly touch the UI on the main thread you can also jank the UI. The fix is to offload the heavy work to a background executor and report results asynchronously. On Android you run it on a coroutine or an Executor/thread pool; on iOS you dispatch to a background DispatchQueue. You return the result through a Promise (resolve/reject) or a callback, so the JS side awaits without blocking and the module queue stays free.

COMMON WRONG ANSWERS Saying native module methods run on the JS thread is incorrect and would imply they block JS execution. Saying they run on the main UI thread by default is also wrong for the general case. Doing a long blocking computation inline on the module queue stalls subsequent native calls. Touching UIKit/Views off the main thread is unsafe.

LIKELY FOLLOW-UPS When must you be on the main thread? For UI work; dispatch back to the main queue/runOnUiThread. How do you return async results? Promises or callbacks. How does the new architecture (TurboModules) differ? Calls can be synchronous over JSI, so threading discipline matters even more.

ONE CONCRETE EXAMPLE A module method that hashes a 50MB file blocks the module queue if run inline, delaying other native calls. Instead, on iOS dispatch the hashing to DispatchQueue.global(), then resolve the promise with the digest; on Android run it in a coroutine on Dispatchers.Default and resolve. JS just awaits the promise.

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.