An MLX Swift demo often looks done. You tap Generate in the Xcode debugger. Tokens stream. You stop the run. That is not a production session. In Xcode the scene stays in the foreground. The GPU stays yours. Memory pressure is mild because you are watching one prompt.
A shipped user does other things. They lock the phone. They switch apps. A call arrives. The scene leaves the foreground while your generate loop is still submitting Metal work.
flowchart LR
U[User prompt] --> G[MLX generate]
G --> M[Metal GPU]
M --> T[Tokens on screen]
MLX Swift runs arrays on Apple silicon through Metal. That is the point of the stack. It is also the failure mode. When the scene is no longer active, iOS can take the GPU away. Command buffers fail. The generate Task throws or hangs. The user sees a half answer and a dead spinner.
flowchart LR
G[MLX generate] --> M[Metal GPU]
B[User backgrounds app] --> S[Scene not active]
S --> M
Memory is the second hit. A 4-bit Gemma-class model plus KV cache is a large resident set. When the app is backgrounded, jetsam can kill the process. You do not get a clean Swift error. You get a relaunch. The in-memory session is gone.
flowchart LR
S[Scene not active] --> GPU[GPU revoked]
S --> J[Jetsam]
GPU --> D[Inference dies]
J --> D
Two other production breaks show up after the demo. A newer family such as Gemma 4 ships a model_type your pinned mlx-swift-lm does not know. The stock loader fails before you ever reach Metal. You will register that type in lesson 03. And if you leave generate running in the background, Metal dies even when the architecture loaded fine. You will gate on scenePhase in lesson 04.
This course is for the case where you already chose MLX Swift as the runtime. Core AI vs Core ML vs MLX still holds: Core AI is the official path for a baked custom neural model in an App Store app. MLX Swift is the open-source path when you need that model zoo in Swift. Production here means the load works for the family you ship, and the GPU is not used after the scene backgrounds.
The same lifecycle idea shows up in agent work. The agent harness (and how to improve it) treats control as a layer you own. Here the control is simple: do not run Metal when the scene is not active.
Next, set up Xcode and the packages before you write a generate call.
Key concepts
- A debugger Run keeps the scene active. A real user backgrounds the app mid-generate.
- MLX Swift uses Metal. iOS can revoke the GPU when the scene is not active.
- Jetsam can kill a high-memory generate process. The in-memory session does not survive.
- A missing
model_typefails at load. A missing scene gate fails at Metal.
Takeaways
- Do not treat a foreground Xcode demo as production.
- Name the two ship bugs: unknown architecture, and Metal work after background.
- Plan to cancel generate when the scene leaves the foreground.