Overview
React Native ExecuTorch uses a ResourceFetcher abstraction to handle downloading and caching of model files, tokenizers, and other resources. The system supports multiple source types and provides progress tracking, pause/resume, and efficient caching.ResourceFetcher Adapter Pattern
The library uses an adapter pattern to support different React Native setups:ResourceFetcherAdapter Interface
All adapters must implement this minimal interface:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcher.ts:18-45
ResourceSource Types
TheResourceSource type accepts multiple formats:
String Sources
Remote URLs:Number Sources (Bundled Assets)
Object Sources (Configuration)
~/workspace/source/packages/react-native-executorch/src/types/common.ts:10
Core ResourceFetcher API
The staticResourceFetcher class delegates to the configured adapter:
~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcher.ts:53-142
Error Handling
If adapter is not initialized:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcher.ts:87-95
ExpoResourceFetcher
For Expo applications (requires development build):Implementation Details
Uses:expo-file-system/legacyfor downloads and file operationsexpo-assetfor bundled asset resolution- Caching directory for temporary downloads
- Document directory for final storage
~/workspace/source/packages/expo-resource-fetcher/src/ResourceFetcher.ts:100-561
Key Methods
Pause/Resume/Cancel:~/workspace/source/packages/expo-resource-fetcher/src/ResourceFetcher.ts:290-321
File Management:
~/workspace/source/packages/expo-resource-fetcher/src/ResourceFetcher.ts:336-381
BareResourceFetcher
For bare React Native applications:Implementation Details
Uses:@dr.pogodin/react-native-fsfor file operations@kesha-antonov/react-native-background-downloaderfor downloadsImage.resolveAssetSource()for bundled assets
~/workspace/source/packages/bare-resource-fetcher/src/ResourceFetcher.ts:517-558
API
Identical to ExpoResourceFetcher:fetch()- Download resourcespauseFetching()- Pause downloadsresumeFetching()- Resume downloadscancelFetching()- Cancel downloadslistDownloadedFiles()- List filesdeleteResources()- Delete files
~/workspace/source/packages/bare-resource-fetcher/src/ResourceFetcher.ts:103-572
Progress Tracking
Progress callbacks report download progress from 0 to 1:Multi-file Progress
When fetching multiple files, progress is weighted by file size:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts:155-181
Caching Strategy
Cache Check
Filename Generation
URLs are converted to safe filenames:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts:204-208
Cache Invalidation
Currently, there’s no automatic cache invalidation. To force re-download:Hugging Face Integration
Downloads from Hugging Face automatically increment download counters:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts:188-197
Resource Types
Source Type Detection
The fetcher automatically detects source types:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts:40-65
Handling Each Type
~/workspace/source/packages/expo-resource-fetcher/src/ResourceFetcher.ts:159-198
Best Practices
- Initialize early: Call
initExecutorch()at app startup - Show progress: Use progress callbacks for large downloads
- Handle interruptions: Check for
nullreturn (download cancelled) - Clean up: Delete unused models to save space
- Error handling: Catch and handle
ResourceFetcherDownloadFailederrors
Next Steps
- Model Loading - Learn how to load models after fetching
- Error Handling - Handle resource fetching errors