Skip to content

Conversation

@bassrock
Copy link
Contributor

@bassrock bassrock commented Nov 25, 2025

Description

This PR adds URL-based asset caching to the ReferencedAssetLoader on both Android and iOS platforms. The implementation introduces a dedicated URLAssetCache module that caches downloaded HTTP/HTTPS assets to disk, improving performance by avoiding redundant network requests for the same assets.

Changes Made

URL Asset Caching

  • New URLAssetCache Module: Created a dedicated caching system for URL-based assets on both platforms
    • Android (URLAssetCache.kt):

      • Uses SHA-256 hashing for cache key generation
      • Cache directory: cacheDir/rive_url_assets/
      • Thread-safe operations with error handling
      • Provides getCachedData(), saveToCache(), and clearCache() methods
    • iOS (URLAssetCache.swift):

      • Uses CryptoKit's SHA256 for cache key generation
      • Cache directory: cachesDirectory/rive_url_assets/
      • Thread-safe operations with silent error handling
      • Provides static methods for cache operations

ReferencedAssetLoader Updates

  • Cache Integration: Both Android and iOS loaders now check the cache before downloading HTTP/HTTPS assets

    • Cache lookup occurs before network requests
    • Downloaded assets are automatically saved to cache
    • Non-URL assets (bundled, resources) bypass the cache and use direct loading
    • Cache operations are transparent to the asset loading flow
  • Asset Cache Management in createCustomLoader:

    • Assets are immediately saved to the cache map when encountered (by both uniqueName and name keys)
    • Enables Dynamic Asset Updates: This allows users to pass in new referencedAssets later in the file lifecycle. Since assets are cached immediately, new referenced assets can be provided and will properly associate with existing file assets throughout the file's lifecycle.

Additional Changes

  • HybridViewModelImageProperty Fix: Fixed listener callback to properly handle value changes by mapping to Unit instead of empty map

Additional Information

Technical Details

  • Cache Key Generation:

    • Android: SHA-256 hash of the URL (64-character hex string)
    • iOS: SHA256 hash using CryptoKit (64-character hex string)
    • Both approaches ensure deterministic, filesystem-safe cache keys
  • Cache Storage:

    • Files are stored with hashed filenames to avoid filesystem issues with special characters in URLs
    • Cache directory is created automatically if it doesn't exist
    • Cache persists across app restarts (stored in app's cache directory)
  • Cache Strategy:

    • Cache-first approach: checks cache before network requests
    • Write-through: downloaded assets are immediately cached
    • Best-effort: cache failures don't block asset loading
    • Only HTTP/HTTPS URLs are cached; other asset sources (bundled, resources) bypass cache
  • Asset Cache Management: The createCustomLoader function now immediately caches all assets (both by uniqueFilename/uniqueName and name) when they are first encountered, regardless of whether they've been loaded yet. This ensures:

    • The cache map is always up-to-date
    • Assets can be updated/reloaded with new referenced asset data
    • New referenced assets can be provided and will properly associate with existing file assets

Breaking Changes

None - this is a backward-compatible enhancement. Existing functionality remains unchanged, with caching added as an optimization layer.

@HayesGordon
Copy link
Contributor

HayesGordon commented Nov 25, 2025

This is great @bassrock! We'll get this reviewed

Edit: and thanks for the contribution + early feedback

}
}

func dispose() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this dispose is called from?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm actually looks like it never is. Guess its a bug

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW we've ended up merging a slightly different fix for the iOS crash. #45

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfazekas I was also seeing a similar issue on Android

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the disposal on android and ios, and rebased on main

@bassrock bassrock force-pushed the db/feat/cdn-cache branch 2 times, most recently from b628586 to 42c36a9 Compare December 8, 2025 20:39
@bassrock bassrock changed the title feat(cdn/viewDispoal): adding in cdn caching and view disposal checking feat(cdn): adding in cdn caching Dec 8, 2025
@bassrock
Copy link
Contributor Author

bassrock commented Dec 8, 2025

@mfazekas I have made updates since the view stuff was fixed elsewhere. This now fixes just referenced assets and adds in a Cache layer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this file be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HayesGordon my understanding is this is an artifact of the Kotlin compiler process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants