今日已更新 351 条资讯 | 累计 41571 条内容
关于我们

Build a React Native video upload that survives backgrounding, app kills, and cellular

Mason K 2026年09月10日 14:55 1 次阅读 来源:Dev.to

TL;DR A fetch() upload dies when the OS suspends your app, and your JS context is recreated with no memory of it. We are going to build an upload that is a persisted record plus a state machine , hand the actual transfer to a native background session, and reconcile against the server when the app comes back. About 150 lines. Versions this was written against: Expo SDK 54 (React Native 0.81) and SDK 55 (React Native 0.83) . Relevant because SDK 54 is the last release with legacy architecture support and SDK 55 is New Architecture only, so any native upload library you pick needs New Architecture support. The failure we are fixing 📱 // the version in every tutorial const res = await fetch ( uploadUrl , { method : ' PUT ' , body : file }); setProgress ( 100 ); Background the app mid-transfer and this stops existing. No error, no rejection, no catch block. iOS suspends the process; Android may reclaim it. When the user returns, your JS runtime is fresh and that promise never settles. Test it yourself before reading further, because it is more convincing than any explanation: # start a large upload, then: # 1. swipe to home # 2. lock the phone # 3. wait 60s # 4. reopen the app # most implementations show a frozen progress bar forever 1. Know what the platforms will and will not do Neither OS lets JavaScript keep a transfer alive. The constraints are worth internalising because they dictate the design. Platform What continues in the background The catch iOS Background URLSession upload task Must upload from a file on disk . Data/stream bodies are not supported in background sessions. Completion-handler convenience APIs are unavailable; use the delegate API. Android Foreground service ( dataSync ) Service type required when targeting Android 14+. Android 15 caps dataSync and mediaProcessing at 6 hours per 24 hours , then calls Service.onTimeout() . Past the budget, starting one throws ForegroundServiceStartNotAllowedException . Two consequences fall straight out: Write th

本文内容来源于互联网,版权归原作者所有
查看原文