Working with Different Threads in Swift TurboModules

Teachers
Oskar Kwaśniewski
Software Developer
@
Callstack

TurboModules give you direct access to native APIs, but performance and correctness depend heavily on where that code runs. In this episode, Oskar Kwaśniewski breaks down the threading model behind React Native TurboModules on iOS and shows how to safely move work across JavaScript, native module, background, and main threads using Swift and Grand Central Dispatch.

Using the Local Storage TurboModule from earlier episodes, Oskar demonstrates how synchronous and asynchronous methods behave, how blocking the JavaScript thread can freeze your app, and how to offload heavy work to background or custom queues while keeping the UI responsive.

Understanding React Native threads on iOS

Oskar starts by mapping out the three core threads you interact with in a React Native app on iOS: the main (UI) thread, the JavaScript thread, and the native modules thread pool. He explains what each thread is responsible for and why accessing UIKit must always happen on the main thread, while heavy computation should never run on the JS thread.

Checking which thread your TurboModule runs on

To make thread behavior visible, Oskar adds simple logging and debugger breakpoints to inspect the current thread at runtime. He shows how synchronous TurboModule methods execute directly on the JavaScript thread, while async methods are dispatched through the TurboModule manager queue.

Why blocking the JavaScript thread is dangerous

By intentionally calling Thread.sleep in a synchronous method, Oskar demonstrates how easily the JavaScript thread can be blocked and how that immediately makes the app unresponsive. This example highlights the most important takeaway of the episode: never perform long‑running work on the JS or main thread.

Introducing Grand Central Dispatch (GCD)

Oskar introduces Grand Central Dispatch as the foundation for concurrency in Swift. Instead of managing threads manually, you submit closures to dispatch queues and let the system schedule work efficiently. He explains how global queues work and how Quality of Service (QoS) affects task priority.

Jumping between background and main threads

Using DispatchQueue.global().async and DispatchQueue.main.async, Oskar shows how to move work off the JavaScript thread, perform background processing, and then safely return to the main thread to update UI‑related state.

Serial vs concurrent custom queues

The episode then dives into custom dispatch queues. Oskar creates a private serial queue to demonstrate predictable, ordered execution, then switches it to a concurrent queue to show how multiple tasks can run in parallel. He explains when each approach makes sense, such as serial access to shared state versus parallel batch processing.

Implementing async TurboModule methods

Finally, Oskar adds asynchronous versions of getItem and setItem to the TurboModule. He walks through the Codegen‑generated Objective‑C interfaces, bridging promise‑based methods to Swift, and resolving promises from a background or custom queue without blocking React Native’s internal thread pools.

Key takeaways

  • Synchronous TurboModule methods run on the JavaScript thread and must be fast.
  • Never block the JavaScript or main thread with heavy work.
  • Use GCD global or custom queues for long‑running tasks.
  • Async TurboModule methods let you safely offload work and resolve results later.

Resources

Insights

Learn more about React Native

Building a Software Factory: Write Specs an Agent Can Build From — cover

Nov 6Article

Building a Software Factory: Write Specs an Agent Can Build From

Learn how to research requirements, resolve open decisions, and write testable specs that AI coding agents can implement without the planning conversation.

 Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback — cover

Nov 4Article

Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback

Plan codebase migrations with coding agents using reviewable requirements, verification checks, staged rollouts, and defined rollback criteria.

Conversational Banking: From Customer Questions to Authorized Actions — cover

Nov 2Article

Conversational Banking: From Customer Questions to Authorized Actions

Explore conversational banking architecture through a card-dispute example, covering tool access, authorization, execution, observability, and human handoff.

How AI Agents Can Cut Manual QA by 30% — cover

Oct 30Article

How AI Agents Can Cut Manual QA by 30%

Explore an agentic QA workflow targeting 30% less manual effort across mobile, web, and TV, with test selection, failure triage, and validation.

Building a Software Factory: The Night My Agents Waited for Me — cover

Oct 28Article

Building a Software Factory: The Night My Agents Waited for Me

Learn how permissions, review limits, and missing product decisions stalled two overnight coding agent runs, and how their maintainer revised the workflow.

Your AI Coding Agent Still Needs an Architecture — cover

Oct 22Article

Your AI Coding Agent Still Needs an Architecture

Use explicit contracts, behavior tests, and dependency checks to guide AI coding agents and review their changes through a TypeScript favorites example.

cover

Oct 15Live Stream

Who Left the Agents Running? React Native Software Factories

Join Kewin Wereszczyński and Szymon Chmal to explore what can go wrong in a React Native software factory overnight and the controls that keep agents on track.

cover

Oct 7Talk

Automating Mobile QA with Cloud Agents

Michał Pierzchała shows how cloud-based QA agents can test real iOS and Android user flows and bring screenshots, recordings, and logs into pull requests.

cover

Oct 1Live Stream

Can Jev Make AI Agent Decisions Faster?

Explore Jev with Kewin Wereszczyński and Szymon Chmal: how it works, its approach to faster agent decisions, and possible local alternatives.

cover

Aug 4Podcast

VisionCamera V5 With Marc Rousavy

Marc Rousavy joins Mike Grabowski to unpack VisionCamera v5, Nitro Modules, the constraints API, migration work, and what changed for React Native camera apps.

cover

Jul 1Podcast

Redraw for Effects Beyond Skia

William Candillon talks about where Redraw fits next to Skia, from physics-based rendering to camera effects, AR, and new UI patterns.

cover

Feb 17Podcast

From Teddy Bears to Voice Agents: Kraen Hansen on Voice AI, Local-First & App Security

What happens when you combine voice agents and teddy bears? A hands-on look at local-first systems, voice AI, and security lessons from poking real apps.

cover

Mar 19Tutorial

The Hard Way vs. The React Native AI SDK Way: Stop Writing Custom Modules for Every Model

Learn how React Native AI uses a unified provider model to switch between cloud and on-device LLMs without rewriting application logic, enabling reliable AI experiences online and offline.

cover

Mar 12Tutorial

Build Smarter Apps: Tool Calling & AI Orchestration Explained

Learn how on-device models in React Native can call tools, interact with external APIs, and return structured outputs you can use directly in application logic.

cover

Mar 5Tutorial

MLC LLM + React Native: On-Device AI Without the Pain

Learn how to run third-party on-device LLMs in React Native using MLC LLM. Choose your models, run them efficiently across platforms, and keep a unified JavaScript API with React Native AI.

cover

Feb 24Tutorial

Intelligent Fallbacks with Apple Intelligence in React Native

Learn how to use Apple’s built-in Foundation Models in React Native apps, reduce memory usage, and enable fast, fully on-device AI with React Native AI Apple.

cover

Feb 10Tutorial

What Is the React Native AI SDK? A Complete Intro & Quickstart

Learn why on-device AI matters for React Native apps, how local LLMs behave offline, and what problems React Native AI is designed to solve from day one.

cover

Jan 29Tutorial

Implementing an Android TurboModule with Kotlin

Learn how to implement a React Native TurboModule on Android, from the TypeScript spec and Codegen to the Kotlin implementation and package registration.

cover

Jan 19Tutorial

Working with Different Threads in Swift TurboModules

Learn how JavaScript, native module, background, and main threads interact in React Native TurboModules, and how to use GCD queues to run async work safely in Swift.

cover

Jan 9Tutorial

How to Add Type-Safe Constants to Swift TurboModules

Learn how to expose typed, immutable constants from a Swift TurboModule using Codegen, and get full end-to-end type safety between native and JavaScript.

cover

Dec 30Tutorial

Adding Event Emitters to Your TurboModule in Swift

Learn how to emit native events from a Swift TurboModule and subscribe to them in JavaScript, including proper memory management and Codegen integration.

cover

Dec 22Tutorial

Writing Your First TurboModule in Swift

Learn how to build a fully working TurboModule in Swift, integrate it with Codegen, and bridge it through Objective‑C inside an Expo app.