faster-image icon indicating copy to clipboard operation
faster-image copied to clipboard

Prefetch does not work on iOS because of a weak reference

Open xanderdeseyn opened this issue 8 months ago • 3 comments

First off thanks for this great library!

Prefetching images on iOS is currently broken because the ImagePrefetcher instance is destroyed immediately after resolving the promise. I fixed this with the following patch-package:

index d653e1c..e8d0e12 100644
--- a/node_modules/@candlefinance/faster-image/ios/FasterImageViewManager.swift
+++ b/node_modules/@candlefinance/faster-image/ios/FasterImageViewManager.swift
@@ -1,6 +1,16 @@
 @objc(FasterImageViewManager)
 final class FasterImageViewManager: RCTViewManager {
 
+  // Store the prefetcher as an instance property
+  private let imagePrefetcher: ImagePrefetcher
+  
+  override init() {
+      // Configure the shared pipeline or create a dedicated one if needed
+     // Using shared pipeline for simplicity here
+      self.imagePrefetcher = ImagePrefetcher(pipeline: ImagePipeline.shared, destination: .memoryCache) // Or .diskCache
+      super.init()
+  }
+  
   override func view() -> (FasterImageView) {
     return FasterImageView()
   }
@@ -27,8 +37,9 @@ final class FasterImageViewManager: RCTViewManager {
   func prefetch(sources: [String],
                 resolve: @escaping RCTPromiseResolveBlock,
                 reject: @escaping RCTPromiseRejectBlock) {
-    let prefetcher = ImagePrefetcher()
-    prefetcher.startPrefetching(with: sources.compactMap(URL.init(string:)))
+    // Use the stored imagePrefetcher instance
+    imagePrefetcher.startPrefetching(with: sources.compactMap(URL.init(string:)))
+    
     resolve(true)
   }
 }

xanderdeseyn avatar Apr 09 '25 16:04 xanderdeseyn

I have a related topic, sorry in advance for placing it here: As I have realized - or it's related to the bug - the prefetching is only into memory, is this correct? I expected it to lead to disc caching as well, which would help me much more than a memory prefetching.

Thus my question/wish would be: Do you think it's complicated to add the cachePolicy options to the prefetch as well. Especially cachePolicy: discNoCacheControl would help a lot to prefetch and cache lateron often used online images.

Suggestion: prefetch([imageURLs], {options}) -> options could be cachePolicy at first.

Update: I created a separate ticket for this https://github.com/candlefinance/faster-image/issues/80

afiller avatar Apr 10 '25 20:04 afiller

Just want to mention that TurboImage had the similar issue before. We already solves this by PR. Besides, it will resolve promise after finishing prefetching. If you are looking for an alternative, fell free to check it out.

duguyihou avatar Apr 17 '25 02:04 duguyihou

Just want to mention that TurboImage had the similar issue before. We already solves this by PR. Besides, it will resolve promise after finishing prefetching. If you are looking for an alternative, fell free to check it out.

@xanderdeseyn, we now switched to TurboImage and are happy that the pre-caching is working there. Thanks. 🥳👍

afiller avatar Apr 28 '25 11:04 afiller