Repository: software-mansion/react-native-svg (language: java)

Issue:
toDataURL only works the second time it is executed in old Arch
Hello,

in iOS and old Arch, I found that the callback on toDataURL method will only be called the second time of asking. This is due to when the queue from UIManager in iOS is flushed. See https://github.com/facebook/react-native/issues/1365 

I traced the problem to the iOS implementation and will do a PR with my fix.

## Environment info

React native info output:

```bash
System:
  OS: macOS 14.3.1
  CPU: (12) arm64 Apple M2 Max
  Memory: 96.39 MB / 32.00 GB
  Shell:
    version: 3.6.0
    path: /opt/homebrew/bin/fish
Binaries:
  Node:
    version: 18.19.1
    path: ~/.nvm/versions/node/v18.19.1/bin/node
  Yarn:
    version: 1.22.21
    path: ~/.nvm/versions/node/v18.19.1/bin/yarn
  npm:
    version: 10.2.4
    path: ~/.nvm/versions/node/v18.19.1/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11330709
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
```

Library version: 14.1.0

## Steps To Reproduce

Simply run the Examples App in this repo with `react-native 0.73.4` and go to "Tap the shapes to render the Image below based on the base64-data of the Svg". If you click once, nothing happens, but if you click a second time, the image is rendered.



Discussion / hints:
Thank you for the issue and the fix. Works well for me 🙏 
> Thank you for the issue and the fix. Works well for me 🙏

No problem! Im glad it helps 😊
Hello @imanderson,
I tried to reproduce the issue, but couldn't.
When I'm using toDataURL everything worked well.

Here is my example:
```
import { SafeAreaView } from 'react-native';
import Svg, { Path} from 'react-native-svg';

const SvgLogoWelcome = (props) => {
  return (
    <Svg
      ref={(ele) => {
        ele.toDataURL((base64Image) => {
          console.log(base64Image, 'data');
        });
      }}
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="black"
      {...props}>
      <Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
    </Svg>
  );
};

function App() {
  return (
    <SafeAreaView>
      <SvgLogoWelcome />
    </SafeAreaView>
  );
}
```
@bohdanprog just to be sure: are you using the old arch in your example?
@buschco Yes, I check the old and new Arch Android and ios platforms.
@bohdanprog the problem only occurs, if you want to create the data url from a button press like so:

```ts
import * as React from 'react';
import {Button, SafeAreaView, View} from 'react-native';
import Svg, {Path} from 'react-native-svg';

const SvgLogoWelcome = props => {
  const ref = React.useRef();
  return (
    <View>
      <Button
        onPress={() => {
          ref.current?.toDataURL(base64Image => {
            console.log(base64Image, 'data');
          });
        }}
        title="log"
      />
      <Svg
        ref={ref}
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 24 24"
        fill="black"
        {...props}>
        <Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
      </Svg>
    </View>
  );
};

function App() {
  return (
    <SafeAreaView>
      <SvgLogoWelcome />
    </SafeAreaView>
  );
}
```
@buschco, thanks for example. I'm going to check that and let you know ;)
@buschco, here is an example, you can test it.
https://github.com/bohdanprog/rnsvg-data-url-test

Let me know if it doesn't work because for me everything worked well. 
Thank you.
@bohdanprog 
same behaviour as described above: the first click wont log the base64, the second one will.

To better demonstrate the issue, I added a `console.log('hi')` above:

```patch
diff --git a/App.js b/App.js
index ab8d424..4adfc19 100644
--- a/App.js
+++ b/App.js
@@ -8,6 +8,7 @@ const SvgLogoWelcome = (props) => {
     <View>
       <Button
         onPress={() => {
+          console.log("hi");
           ref.current?.toDataURL((base64Image) => {
             console.log(base64Image, "data", Platform.OS);
           });
```

https://github.com/user-attachments/assets/68bc4122-5041-48b4-a643-7c80765bb33b


My bad, you're right, sorry, I thought I misclicked on that button that's why I clicked twice. 
I will check that PR and let you know, again thanks for explaining the main problem.

Fix patch:
diff --git a/apple/RNSVGSvgViewModule.mm b/apple/RNSVGSvgViewModule.mm
index 3d8696150..5b72f0192 100644
--- a/apple/RNSVGSvgViewModule.mm
+++ b/apple/RNSVGSvgViewModule.mm
@@ -26,54 +26,47 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag
          callback:(RCTResponseSenderBlock)callback
           attempt:(int)attempt
 {
-  void (^block)(void) = ^{
 #ifdef RCT_NEW_ARCH_ENABLED
-    [self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
-      __kindof RNSVGPlatformView *view = [viewRegistry viewForReactTag:reactTag];
+  [self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
+    __kindof RNSVGPlatformView *view = [viewRegistry viewForReactTag:reactTag];
 #else
-    [self.bridge.uiManager
-        addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, RNSVGPlatformView *> *viewRegistry) {
-          __kindof RNSVGPlatformView *view = [uiManager viewForReactTag:reactTag];
+  [self.bridge.uiManager
+      addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, RNSVGPlatformView *> *viewRegistry) {
+        __kindof RNSVGPlatformView *view = [uiManager viewForReactTag:reactTag];
 #endif // RCT_NEW_ARCH_ENABLED
-      NSString *b64;
-      if ([view isKindOfClass:[RNSVGSvgView class]]) {
-        RNSVGSvgView *svg = view;
-        if (options == nil) {
-          b64 = [svg getDataURLWithBounds:svg.boundingBox];
-        } else {
-          id width = [options objectForKey:@"width"];
-          id height = [options objectForKey:@"height"];
-          if (![width isKindOfClass:NSNumber.class] || ![height isKindOfClass:NSNumber.class]) {
-            RCTLogError(@"Invalid width or height given to toDataURL");
-            return;
-          }
-          NSNumber *w = width;
-          NSInteger wi = (NSInteger)[w intValue];
-          NSNumber *h = height;
-          NSInteger hi = (NSInteger)[h intValue];
-
-          CGRect bounds = CGRectMake(0, 0, wi, hi);
-          b64 = [svg getDataURLWithBounds:bounds];
-        }
-      } else {
-        RCTLogError(@"Invalid svg returned from registry, expecting RNSVGSvgView, got: %@", view);
-        return;
-      }
-      if (b64) {
-        callback(@[ b64 ]);
-      } else if (attempt < 1) {
-        [self toDataURL:reactTag options:options callback:callback attempt:(attempt + 1)];
+    NSString *b64;
+    if ([view isKindOfClass:[RNSVGSvgView class]]) {
+      RNSVGSvgView *svg = view;
+      if (options == nil) {
+        b64 = [svg getDataURLWithBounds:svg.boundingBox];
       } else {
-        callback(@[]);
+        id width = [options objectForKey:@"width"];
+        id height = [options objectForKey:@"height"];
+        if (![width isKindOfClass:NSNumber.class] || ![height isKindOfClass:NSNumber.class]) {
+          RCTLogError(@"Invalid width or height given to toDataURL");
+          return;
+        }
+        NSNumber *w = width;
+        NSInteger wi = (NSInteger)[w intValue];
+        NSNumber *h = height;
+        NSInteger hi = (NSInteger)[h intValue];
+
+        CGRect bounds = CGRectMake(0, 0, wi, hi);
+        b64 = [svg getDataURLWithBounds:bounds];
       }
-    }];
-  };
-  if (self.bridge) {
-    dispatch_async(RCTGetUIManagerQueue(), block);
-  } else {
-    dispatch_async(dispatch_get_main_queue(), block);
-  }
-}
+    } else {
+      RCTLogError(@"Invalid svg returned from registry, expecting RNSVGSvgView, got: %@", view);
+      return;
+    }
+    if (b64) {
+      callback(@[ b64 ]);
+    } else if (attempt < 1) {
+      [self toDataURL:reactTag options:options callback:callback attempt:(attempt + 1)];
+    } else {
+      callback(@[]);
+    }
+  }];
+};
 
 RCT_EXPORT_METHOD(toDataURL
                   : (nonnull NSNumber *)reactTag options
@@ -91,4 +84,13 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag
 }
 #endif
 
+- (dispatch_queue_t)methodQueue
+{
+  if (self.bridge) {
+    return RCTGetUIManagerQueue();
+  } else {
+    return dispatch_get_main_queue();
+  }
+}
+
 @end


Test patch:
diff --git a/apps/test-examples/App.js b/apps/test-examples/App.js
index c5a50dc9c..2e76aee77 100644
--- a/apps/test-examples/App.js
+++ b/apps/test-examples/App.js
@@ -22,6 +22,7 @@ import Test2248 from './src/Test2248';
 import Test2266 from './src/Test2266';
 import Test2276 from './src/Test2276';
 import Test2327 from './src/Test2327';
+import Test2233 from './src/Test2233';
 import Test2366 from './src/Test2366';
 
 export default function App() {
diff --git a/apps/test-examples/src/Test2233.tsx b/apps/test-examples/src/Test2233.tsx
new file mode 100644
index 000000000..4ba5dd14f
--- /dev/null
+++ b/apps/test-examples/src/Test2233.tsx
@@ -0,0 +1,30 @@
+import * as React from 'react';
+import {Button, SafeAreaView, View} from 'react-native';
+import Svg, {Path} from 'react-native-svg';
+
+const SvgLogoWelcome = () => {
+  const ref = React.useRef<Svg | null>(null);
+  return (
+    <View>
+      <Button
+        onPress={() => {
+          ref.current?.toDataURL(base64Image => {
+            console.log(base64Image, 'data');
+          });
+        }}
+        title="log"
+      />
+      <Svg ref={ref} viewBox="0 0 24 24" fill="black">
+        <Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
+      </Svg>
+    </View>
+  );
+};
+
+export default function App() {
+  return (
+    <SafeAreaView>
+      <SvgLogoWelcome />
+    </SafeAreaView>
+  );
+}