customerio / customerio-reactnative

MIT License
23 stars 11 forks source link

In-App Messaging Crash - main thread must be used #161

Closed djw27 closed 1 year ago

djw27 commented 1 year ago

SDK version: 2.4.2

Environment: Development

Describe the bug With in-app messaging enabled & with page rules setup as the trigger for displaying the in-app message, the app crashes when trying to show the in-app message.

When logging screen views with customerio, i.e. cio.screen('ScreenName') we're doing so on a background thread. When this eventually calls through to setCurrentRoute of Gist and ultimately invokes a MessageManager which tries to initialise a WKWebView we get the error -[WKWebView .cxx_construct] must be used from main thread only.

To Reproduce

Screenshots

Screenshot 2023-07-06 at 11 04 28

Additional context I can see that the library makes use of DispatchQueue.main.async to execute tasks on the main thread when initializing the library and registering for push notifications.

I can verify that wrapping the main CustomerIO.shared.screen(name: name) call with this 'fixes' the problem, i.e.

func screen(name : String, data : Dictionary<String, AnyHashable>?) -> Void {
...
  DispatchQueue.main.async {
    CustomerIO.shared.screen(name: name)
  }
...
}

To be a bit more specific, the following also works by overriding specifically where we bridge from customerio -> Gist:

extension MessagingInAppImplementation: ScreenTrackingHook {
    public func screenViewed(name: String) {
        logger.debug("setting route for in-app to \(name)")

        DispatchQueue.main.async {
            self.inAppProvider.setRoute(name)
        }
    }
}

I'm not sure on the implications of this as a solution, though. Thoughts? I would have thought that this issue would be impacting everyone trying to use in-app messaging so a little surprised it hasn't been raised before, unless we're doing something obviously incorrect?

Shahroz16 commented 1 year ago

Hey @djw27, thank you for reporting this. We are having a look into this and will report back with the findings. Apologies for inconvenience.