Skip to content

Prefetch does not work on iOS because of a weak reference #79

Description

@xanderdeseyn

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)
   }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions