React Native
Model Management API
NEW in v2.0.2: The React Native Vision SDK includes a comprehensive Model Management API that provides fine-grained control over on-device ML models. This replaces the deprecated loadModel() and unLoadModel() methods with a more powerful and flexible system.
Overview
The Model Management API allows you to:
- Download models from the server to disk with progress tracking
- Load models into memory for fast inference
- Unload models from memory to free resources
- Delete models from disk permanently
- Query model status (downloaded, loaded, size, version)
- Switch between different models dynamically
- Track lifecycle events for all model operations
Key Benefits
- Separation of Concerns: Download and load are separate operations
- Progress Tracking: Per-download progress callbacks with unique request IDs
- Model Caching: Download once, load multiple times without re-downloading
- Memory Control: Unload without deleting, or delete permanently
- Type Safety:
OCRModuletype instead of separate string parameters - Explicit Selection: Specify exact model for predictions
Quick Start
Initialization
initializeModelManager(config)
Initialize the Model Manager singleton with configuration options.
Parameters
config(ModelManagerConfig):maxConcurrentDownloads?: number- Maximum concurrent downloads (default: 2)enableLogging?: boolean- Enable debug logging (default: true)
Platform Requirements
- Android: REQUIRED - Must be called before any model operations
- iOS: Not needed - This method is a hardcoded no-op for API consistency
Example
Android: Model operations will fail if initializeModelManager() is not called first.
iOS: This method exists only for cross-platform API consistency and has no effect.
isModelManagerInitialized()
Check if the Model Manager has been initialized.
Returns
boolean-trueif initialized,falseotherwise
Example
iOS: Always returns true (hardcoded). iOS doesn't require initialization.
Download Operations
downloadModel(module, apiKey, token, platformType, progressCallback)
Download a model from the server to device storage with progress tracking.
Parameters
module(OCRModule): Model to downloadtype:'shipping_label'|'item_label'|'bill_of_lading'|'document_classification'size:'nano'|'micro'|'small'|'medium'|'large'|'xlarge'
apiKey(string | null): API key for authenticationtoken(string | null): Auth token for authenticationplatformType(string, optional): Platform identifier (default:'react_native')progressCallback(function, optional): Callback for progress updates
Returns
Promise<void>- Resolves when download completes
Progress Callback
The callback receives a DownloadProgress object:
Example
Error Handling
cancelDownload(module)
Cancel an active download operation for a specific model.
Parameters
module(OCRModule): The model whose download to cancel
Returns
Promise<boolean>-trueif cancelled,falseif no active download
Example
Load/Unload Operations
loadOCRModel(module, apiKey, token, platformType, executionProvider)
Load a model from disk into memory for inference.
Parameters
module(OCRModule): Model to loadapiKey(string | null): API keytoken(string | null): Auth tokenplatformType(string, optional): Platform identifier (default:'react_native')executionProvider(ExecutionProvider, optional): Android only'CPU'(default) - Best compatibility, works on all devices'NNAPI'- Android Neural Networks API for hardware acceleration'XNNPACK'- Optimized CPU kernels for ARM processors
Returns
Promise<void>- Resolves when loaded
Example
If the model is not downloaded, it will be automatically downloaded first before loading.
unloadModel(module)
Unload a model from memory. Files remain on disk for faster reloading.
Parameters
module(OCRModule): Model to unload
Returns
Promise<boolean>-trueif unloaded,falseif wasn't loaded
Example
isModelLoaded(module)
Check if a specific model is currently loaded in memory.
Parameters
module(OCRModule): Model to check
Returns
boolean-trueif loaded,falseotherwise
Example
getLoadedModelCount()
Get the number of models currently loaded in memory.
Returns
number- Count of loaded models
Example
Query Operations
findDownloadedModels()
List all models downloaded to device storage.
Returns
Promise<ModelInfo[]>- Array of downloaded models
ModelInfo Structure
Example
findDownloadedModel(module)
Find information about a specific downloaded model.
Parameters
module(OCRModule): Model to find
Returns
Promise<ModelInfo | null>- Model info if found,nullotherwise
Example
findLoadedModels()
List all models currently loaded in memory.
Returns
Promise<ModelInfo[]>- Array of loaded models
Example
Delete Operations
deleteModel(module)
Permanently delete a model from disk. If the model is loaded, it will be unloaded first.
Parameters
module(OCRModule): Model to delete
Returns
Promise<boolean>-trueif deleted,falseif not found
Example
Deletion is permanent. The model must be re-downloaded to use again.
Prediction with Specific Models
predictWithModule(module, imagePath, barcodes)
Perform OCR prediction using a specific model.
Parameters
module(OCRModule): The model to use for predictionimagePath(string): Path to image file or URIbarcodes(string[]): Array of barcode strings detected in image
Returns
Promise<any>- Prediction result (structure varies by model type)
Example
The model must be loaded into memory before calling this method.
Event Listeners
onModelLifecycle(listener)
Subscribe to global model lifecycle events.
Parameters
listener(function): Callback for lifecycle events
Returns
EmitterSubscription- Subscription object (call.remove()to unsubscribe)
Event Types
onDownloadStarted- Model download beganonDownloadCompleted- Model download finishedonDownloadFailed- Model download failedonDownloadCancelled- Model download was cancelledonModelLoaded- Model loaded into memoryonModelUnloaded- Model unloaded from memoryonModelDeleted- Model deleted from disk
Example
Complete Examples
Download Multiple Models Concurrently
Switch Between Models
Check Before Operations
Full Workflow with State Management
Platform Differences
iOS vs Android
Android-Specific Behavior
Initialization Requirement:
Android requires explicit initialization before any model operations:
iOS-Specific Behavior
Initialization Not Required:
iOS does not require initialization. The methods exist only for API consistency:
initializeModelManager()- Does nothing (hardcoded no-op)isModelManagerInitialized()- Always returnstrue(hardcoded)
You can safely call these methods for cross-platform code consistency.
Migration from Deprecated API
Old API (Deprecated)
New API (Recommended)
Benefits of New API
- Separate download and load phases
- Progress tracking per download
- Model caching (download once, load multiple times)
- Clear separation: unload vs delete
- Boolean return values for status checking
- Type-safe OCRModule parameter
- Explicit model selection for predictions
Best Practices
1. Initialize Once (Android Only)
2. Check Before Operations
3. Clean Up Unused Models
4. Handle Errors Gracefully
5. Monitor Lifecycle Events
Types & Interfaces
OCRModule
ModelManagerConfig
DownloadProgress
ModelInfo
ExecutionProvider (Android only)
Recommendations:
CPU: Best compatibility, works on all devicesNNAPI: Potentially faster on supported devices (Android 8.1+)XNNPACK: Optimized for ARM processors
Related Documentation
- VisionCore Module - Complete VisionCore API reference
- Headless OCR Prediction - Camera-independent OCR workflows
- AI Scanning (On-Device) - On-device OCR with camera
- Setup Guide - Initial SDK setup
API Reference Summary
Last Updated: December 2024 SDK Version: 2.0.2+