PostHog / posthog-ios

PostHog iOS SDK
https://posthog.com/docs/libraries/ios
MIT License
35 stars 41 forks source link

PHGPostHog.setup(with: configuration) only on Main Thread #58

Closed xxZap closed 11 months ago

xxZap commented 1 year ago

With the latest 2.0.3 version it seems like when you call the method to initialize the library if this is not called on the main thread, the console is filling with tons of errors.

As the title suggests, the method that is causing these errors is PHGPostHog.setup(with: configuration)

Main Thread Checker: UI API called on a background thread: -[UIApplication connectedScenes]
PID: 43007, TID: 2562761, Thread name: (none), Queue name: SerialQueue, QoS: 25
Backtrace:
4   XXXXX                             0x00000001050f994c -[PHGApplicationUtils windows] + 208
5   XXXXX                             0x00000001050fd150 -[PHGPostHogIntegration staticContext] + 1256
6   XXXXX                             0x00000001050fc974 -[PHGPostHogIntegration initWithPostHog:httpClient:fileStorage:userDefaultsStorage:] + 560
7   XXXXX                             0x00000001050f0634 -[PHGPayloadManager initWithPostHog:] + 1020
8   XXXXX                             0x00000001050f4110 -[PHGPostHog initWithConfiguration:] + 352
9   XXXXX                             0x00000001050f3f90 __37+[PHGPostHog setupWithConfiguration:]_block_invoke + 56
10  libdispatch.dylib                   0x00000001092927c0 _dispatch_client_callout + 20

11  libdispatch.dylib                   0x0000000109294364 _dispatch_once_callout + 136
12  XXXXX                             0x00000001050f3f1c +[PHGPostHog setupWithConfiguration:] + 184
...
...
...
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIScene delegate]
PID: 43007, TID: 2562761, Thread name: (none), Queue name: SerialQueue, QoS: 25
Backtrace:
4   XXXXX                             0x00000001050f9a80 -[PHGApplicationUtils windows] + 516
5   XXXXX                             0x00000001050fd150 -[PHGPostHogIntegration staticContext] + 1256
6   XXXXX                             0x00000001050fc974 -[PHGPostHogIntegration initWithPostHog:httpClient:fileStorage:userDefaultsStorage:] + 560
7   XXXXX                             0x00000001050f0634 -[PHGPayloadManager initWithPostHog:] + 1020
8   XXXXX                             0x00000001050f4110 -[PHGPostHog initWithConfiguration:] + 352
9   XXXXX                             0x00000001050f3f90 __37+[PHGPostHog setupWithConfiguration:]_block_invoke + 56
10  libdispatch.dylib                   0x00000001092927c0 _dispatch_client_callout + 20
11  libdispatch.dylib                   0x0000000109294364 _dispatch_once_callout + 136
12  XXXXX                             0x00000001050f3f1c +[PHGPostHog setupWithConfiguration:] + 184
...
...
...

It seems like the setup method is now calling some UIApplication's method.

Since this behaviour is not indicated anywhere nor in the internal or external documentation, I'm actually using this API on a parallel Thread and this is causing the errors.

Is this side effect to be considered a new requirement to use your library, or is it actually a bug?

Otherwise, I have to rethink all the way I'm initiating the PostHog component

marandaneto commented 12 months ago

Some features require to be run in the main thread, but we should not crash anyway but rather do not install the features, so it's a bug. I'd recommend using the main thread for now.

marandaneto commented 11 months ago

https://github.com/PostHog/posthog-ios/releases/tag/3.0.0-alpha.2 should have fixed this issue, just tested it by initing the new SDK as:

private let dispatchQueue = DispatchQueue(label: "com.posthog.test", target: .global(qos: .utility))
dispatchQueue.async {
  // init iOS SDK v3
}