Skip to main content
Fabien Fouassier
Back to Writing

React Native Integrations: Own the Native Lifecycle

A document-conversion job shows what has to be decided when a mobile app hands work to native code: who owns it, what happens to a late result, and what a test can and cannot prove.

Topics
React NativeSDKArchitecture
Published
Reading time
6 min read

A mobile app often relies on a library written for the phone itself, outside the app's main code. That connection can look correct on paper and still go wrong when the person leaves the screen. The work keeps running, and a result arrives for a screen that now shows something else. Nothing in the connection says who owns that result, or who stops the work behind it.

I would design that lifecycle before deciding how much of the library to expose. React Native's current workflow for native modules uses a typed description to generate the connecting code. That helps define the language boundary, but the application still has to decide who owns the work and what "finished" means. The official native-module guide (opens in new tab) describes the workflow.

Give the app a deliberate contract

Consider a native library that converts a document the user picked. The contract the app sees could be small: start a job, ask to cancel it, and listen for a final result. A job ends in exactly one of three ways: completed (with the converted file), cancelled, or failed (with a reason the app understands).

Two words in that contract need precise meanings. "Started" means the native side accepted the job, not that it finished. "Cancellation accepted" means the request was received, not that the work has stopped. Those meanings belong in the contract itself, because a caller can easily read either answer as "done".

I would keep the vendor's own vocabulary behind the connection and offer the app a short list of states it can handle. Mirroring the whole library is cheaper at first and gives access to everything. It also ties the app to the vendor's terms, inconsistent errors and features the product may never use. A narrower layer costs some translation, but it gives the app something it can reason about and replace.

Decide who owns the job before it starts

The owner creates an identifier for the job and starts listening before the job begins. Otherwise a fast completion could arrive before anyone is there to hear it. Every result carries that identifier, because the fact that a screen exists does not prove a result belongs to its current job.

Listening and doing the work are separate things. Stopping to listen should not cancel work that someone else still needs, and asking to cancel should not remove the only listener who will clean up after the final result.

For a conversion owned by a single screen, I would set out who does what at each stage:

  • Starting: keep listening, and be ready for both a refused start and a result that arrives immediately.
  • Running: accept results only for the job this owner started.
  • Cancel requested: do not promise the work has stopped; keep listening for the final outcome.
  • Finished: settle the job exactly once and release what was used to follow it.
  • Screen closed: stop updating that screen, then hand the job over or release it according to the product's rule.

A conversion the user expects to finish might belong to a service that outlives the screen. Leaving the screen then only drops the screen's listener, while the service keeps following the job. A disposable preview can belong to the screen and ask to cancel on exit. Figure 1 follows the same job under each owner. Neither lifetime is right in general; the product's expected behavior decides.

Read each owner top to bottom. The markers show who is listening: S is the screen, V the service.

Job started

Identifier created, listener attached first

Started means accepted, not finished.

Screen owns the job

S

Running

S

Screen closed

Asks to cancel, keeps listening

S

Final result

Settled once, then released

Service owns the job

SV

Running

V

Screen closed

Drops the screen's listener

V

Final result

Settled once, by the service

Figure 1. Who owns the job Acknowledging a cancellation request does not prove the work has stopped.

Ignoring a late result does not stop the work

A flag or a removed listener can stop a closed screen from reacting. Neither shows that the native library has let go of what it holds (Figure 2).

Stopped by the app

Screen updates

By a flag in the app's code

Results reaching the screen

By removing the listener

Held by the native library

A file

A worker

A pending request

Released only by the native layer's own cleanup; neither app action confirms it.

Figure 2. What the app can stop, and what it cannot see Stopping screen updates or removing a listener does not release native resources.

The native layer needs its own cleanup. If the library can cancel, define how the request maps onto it and which final state confirms it. If it cannot, say so, and decide whether the job continues in the background, whether another job may start, and who sees a failure once the original screen is gone.

Timeouts raise the same issue. A caller that gives up after a deadline has not shown the native work has ended. Starting another job straight away can create overlapping work if the library handles only one at a time. The layer should enforce that limit rather than relying on the screen to remember it.

The native module's own lifetime is a separate boundary again. React Native documents hooks for when a module is set up and torn down, including cleanup when the whole runtime is destroyed. The native-module lifecycle guide (opens in new tab) describes them.

Make failures useful to the caller

An error from native code should tell the app what it can do next. A refused permission, bad input, an unsupported cancellation and an unexpected failure each need a different response. Copying the vendor's raw error text into a message couples the interface to wording that can change, and may expose a file path or document detail.

I would translate known failures into documented codes, keep what the user reads in the app's own copy, and record only the fields needed to investigate. Unknown failures must stay visible. Turning every error into an empty success erases the distinction the caller needs.

The contract also has to say whether a refused start can still be followed by a final result, and how duplicate results are handled. Here I would require exactly one final result per accepted job and reject a repeated job identifier. The connecting layer has to enforce those rules; describing them in a type does not.

Test each layer for what it can prove

A fake native layer written in JavaScript is good for forcing awkward sequences: completion before the start answer returns, a late result after the screen has closed, duplicate final results, cancellation racing with success, and a leftover failure from an earlier job arriving after a new one begins. These tests can show how the app's side decides ownership and updates its state.

They cannot show that native resources were released, which thread a result arrives on, or what happens when the app goes to the background. Native tests and checks on a real device need to cover the actual implementation, the runtime shutting down, permission changes and the library's true completion behavior. If hardware is involved, that adds another thing to test, and a simulator cannot supply evidence about a device it does not reproduce.

A reliable integration needs agreement on ownership, final states and cleanup on both sides of the boundary, followed by evidence from the layers where those behaviors actually run.