BILDIT-Platform / react-native-bildit-flybuy

React Native FlyBuy module. Supports Core, Pickup, Notify, and Presence Native SDK APIs.
9 stars 6 forks source link

Android eventEmitter Listener Error - java.lang.Double cannot be cast to java.lang.String #90

Open azrielh opened 2 months ago

azrielh commented 2 months ago

Describe the bug When using the eventEmitter.addListener for android builds. We get a system crash when there is an orderUpdated trigger. with the error java.lang.Double cannot be cast to java.lang.String

The eventEmitter works fine on iOS, just an issue with Android.

It could be related to the incoming event.partnerIdentifier (and orderId) as it is numeric so in java it may be getting coerced to a Double instead of a string. ie 29344426606903296

Example in useEffect:

useEffect(() => {
    const eventListener = Flybuy.eventEmitter.addListener(
      'orderUpdated',
      (event: IOrder) => {
        if (event.partnerIdentifier === orderId) {
          setFlybuyOrder(event)
        }
      }
    )

    return () => {
      eventListener.remove()
    }
  }, [orderId])

Forcing the event.partnerIdentifier or orderId from above to a string does not fix the issue, as it seems to be in the native code.

Expected behavior When adding the eventEmitter.addListener it should trigger when there is an update to an order to both android and iOS builds.

Screenshots

Additional context

System:
  OS: macOS 14.6.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 119.11 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.12.1
    path: ~/.asdf/installs/nodejs/18.12.1/bin/node
  Yarn:
    version: 1.22.22
    path: /opt/homebrew/bin/yarn
  npm:
    version: 8.19.2
    path: ~/.asdf/plugins/nodejs/shims/npm
  Watchman:
    version: 2024.08.19.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods: Not Found
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.1 AI-241.15989.150.2411.11948838
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.12
    path: /opt/homebrew/opt/openjdk@17/bin/javac
  Ruby:
    version: 2.7.7
    path: /Users/az/.asdf/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.3
    wanted: 0.74.3
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: false
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
addingama commented 2 months ago

That kind of listener will not work on the latest version

Can you try this code

useEffect(() => {
    const eventEmitter = new NativeEventEmitter(NativeModules.RnFlybuyCore);
    const listener = eventEmitter.addListener(
      'orderUpdated',
      (event: IOrder) => {
        console.log('order updated', event);
      },
    );
    return () => {
      listener.remove();
    };
  }, []);
addingama commented 2 months ago

@azrielh please try the latest version

Sample code

import * as FlyBuyCore from 'react-native-bildit-flybuy-core';
import {
  NativeEventEmitter,
  NativeModules,
} from 'react-native';

// inside function component
React.useEffect(() => {
    FlyBuyCore.startObserver();
    return () => {
      FlyBuyCore.stopObserver();
    };
  }, []);

  useEffect(() => {
    const eventEmitter = new NativeEventEmitter(NativeModules.RnFlybuyCore);
    const listener = eventEmitter.addListener(
      'orderUpdated',
      (event: FlyBuyCore.IOrder) => {
        console.log('order updated', event);
      },
    );
    return () => {
      listener.remove();
    };
  }, []);
addingama commented 2 months ago

@azrielh try the latest patch 2.20.9

mohanenm commented 2 months ago

@addingama, that update cannot happen right now due to https://github.com/BILDIT-Platform/react-native-bildit-flybuy/issues/91