# Live patch

Fix the app while people are using it, and put each user back where they were.

Normal updates apply on the next launch, so during a sale or a live event, people keep the bug until they
reopen the app. A live patch applies **now**: the app reloads in about a second and comes back on the
same screen, with the same cart and the same half-filled form.

## How it works

1. You publish with `--live` (or press **Push now → Apply now** on a release).
2. A silent push reaches the devices. The SDK downloads the update and waits for a **safe moment**: the
   app is in the foreground and nothing is holding it (for example, a payment in progress).
3. It saves the state you registered, reloads the JavaScript app, and hands that state back.

```sh
npx healkite release -c production-… -p android -r 2.4 --live
```

Live patch uses [instant hotfix](/instant-updates) delivery, so connect Firebase first.

React Native can't swap code inside a running production app: Metro's module system never redefines a
module that has already loaded, and production builds don't include Fast Refresh. So a live patch is a
fast reload that keeps state, not a change to the running code.

## Keep the user's place

Register what should survive the reload. Anything JSON-serializable works.

**Screen (React Navigation):**

```tsx
import Healkite from "react-native-healkite";

const navigationRef = useNavigationContainerRef();
useEffect(() => Healkite.preserve("navigation", () => navigationRef.getRootState()), []);

<NavigationContainer ref={navigationRef} initialState={Healkite.restored("navigation")}>
```

**Component state:** swap `useState` for `usePreservedState` with a key:

```tsx
import { usePreservedState } from "react-native-healkite";

const [cart, setCart] = usePreservedState("cart", []);
const [address, setAddress] = usePreservedState("checkout.address", "");
```

**A store (Redux, Zustand…):**

```ts
Healkite.preserve("store", () => store.getState());
const initial = Healkite.restored("store"); // pass to your store on start-up
```

State is kept on the device for at most 2 minutes and 256 KB, and it's read once, on the launch right
after the patch. Don't put secrets in it.

## Don't interrupt

Hold the patch during anything that must not be interrupted. Waiting patches apply as soon as nothing
holds them; if something holds one for more than 10 minutes, the update applies on the next launch
instead.

```ts
const release = Healkite.holdLivePatch("payment");
try {
  await pay();
} finally {
  release();
}
```

Show a message if you like:

```ts
Healkite.subscribeLivePatch((phase) => {
  if (phase === "applying") showToast("Updating…");
});
```

## When to use it

- Urgent fixes: checkout broken during a sale, a wrong price, a crash on a busy screen.
- Not for every release. A reload is still visible for a second; regular releases apply on the next launch.

On a device that crashes at start-up after the reload, the usual [rollback on the device](/recovery)
applies: the next launch goes back to the previous bundle.
