LocalStorageProvider.swift
LocalStorageProvider Documentation
Overview
LocalStorageProvider.swift
implements the StorageProvider
protocol using local file storage. It manages entries and clusters by storing them as JSON files in the app's documents directory.
Core Components
Properties
fileManager
System file operations
entriesURL
Location for entries file
clustersURL
Location for clusters file
Initialization
Creates storage directory if needed
Sets up file URLs
Throws if setup fails
Primary Operations
Entry Management
Save Entry
Steps:
Fetch existing entries
Append new entry
Encode to JSON
Write to file
Fetch Entries
Checks file existence
Reads and decodes data
Returns empty array if no file
Cluster Management
Update Cluster
Updates existing or adds new
Maintains cluster list
Persists changes
Fetch Clusters
Similar to entry fetching
Handles missing file case
Decodes cluster data
Helper Methods
JSON Encoding
Generic encoding method
Handles date formatting
Type-safe operation
JSON Decoding
Generic decoding method
Consistent date handling
Type inference
Usage Examples
Basic Operations
Error Handling
File Structure
Directory Layout
File Format
Best Practices
Data Integrity
Atomic write operations
Validate JSON structure
Handle file corruption
Implement backups
Performance
Batch similar operations
Minimize disk access
Cache when appropriate
Handle large files
Error Recovery
Keep backup copies
Validate before writing
Handle partial failures
Clean up temporary files
Concurrency
Use async/await
Handle file locks
Prevent race conditions
Manage shared access
Limitations
Storage Constraints
Local storage only
File size limits
Device space limits
No remote sync
Performance Issues
Full file reads/writes
JSON parsing overhead
Memory usage with large files
No indexing support
Concurrency Limits
Single file access
No concurrent writes
Potential blocking
Lock management
Related Components
StorageProvider: Protocol definition
Entry/Cluster Models: Data structures
JSONEncoder/Decoder: Data conversion
FileManager: File operations
Last updated