quantcast / ios-measurement

Quantcast Measure for iOS
16 stars 9 forks source link

UI API call from a background thread causes issues in Xcode 9 #29

Closed kirsplatrick closed 7 years ago

kirsplatrick commented 7 years ago

Using the latest version of Quantcast in Xcode 9, we are seeing an issue where the framework calls a UI API method (specifically UIApplication applicationState) within a background thread. This causes a runtime warning when running the application but also a crash (log below) on running unit tests.

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 30094, TID: 601744, Thread name: (none), Queue name: com.quantcast.measure.eventQueue (QOS: UNSPECTest Case 'testExample' started.
IFIED), QoS: 0
Backtrace:
4   Quantcast_Measure                   0x00000001125c2fb9 -[QuantcastMeasurement checkSessionID] + 217
5   Quantcast_Measure                   0x00000001125c50af __144-[QuantcastMeasurement internalBeginSessionWithAPIKey:attributedNetwork:userIdentifier:appLabels:networkLabels:appIsDeclaredDirectedAtChildren:]_block_invoke + 1103
6   Quantcast_Measure                   0x00000001125ca744 __48-[QuantcastMeasurement launchOnQuantcastThread:]_block_invoke + 36
7   Foundation                          0x00000001112849b7 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
8   Foundation                          0x000000011128481a -[NSBlockOperation main] + 68
9   Foundation                          0x0000000111282cd6 -[__NSOperationInternal _start:] + 778
10  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
11  libdispatch.dylib                   0x000000011b3cd565 _dispatch_block_invoke_direct + 324
12  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
13  libdispatch.dylib                   0x000000011b3cd565 _dispatch_block_invoke_direct + 324
14  libdispatch.dylib                   0x000000011b3cd401 dispatch_block_perform + 109
15  Foundation                          0x000000011127ece4 __NSOQSchedule_f + 342
16  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
17  libdispatch.dylib                   0x000000011b3ce232 _dispatch_continuation_pop + 500
18  libdispatch.dylib                   0x000000011b3cca93 _dispatch_async_redirect_invoke + 760
19  libdispatch.dylib                   0x000000011b3d2a72 _dispatch_root_queue_drain + 568
20  libdispatch.dylib                   0x000000011b3d27dc _dispatch_worker_thread3 + 119
21  libsystem_pthread.dylib             0x000000011b8835a2 _pthread_wqthread + 1299
22  libsystem_pthread.dylib             0x000000011b88307d start_wqthread + 13
2017-09-26 15:47:26.965525-0500 BuzzFeed[30094:601744] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 30094, TID: 601744, Thread name: (none), Queue name: com.quantcast.measure.eventQueue (QOS: UNSPECIFIED), QoS: 0
Backtrace:
4   Quantcast_Measure                   0x00000001125c2fb9 -[QuantcastMeasurement checkSessionID] + 217
5   Quantcast_Measure                   0x00000001125c50af __144-[QuantcastMeasurement internalBeginSessionWithAPIKey:attributedNetwork:userIdentifier:appLabels:networkLabels:appIsDeclaredDirectedAtChildren:]_block_invoke + 1103
6   Quantcast_Measure                   0x00000001125ca744 __48-[QuantcastMeasurement launchOnQuantcastThread:]_block_invoke + 36
7   Foundation                          0x00000001112849b7 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
8   Foundation                          0x000000011128481a -[NSBlockOperation main] + 68
9   Foundation                          0x0000000111282cd6 -[__NSOperationInternal _start:] + 778
10  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
11  libdispatch.dylib                   0x000000011b3cd565 _dispatch_block_invoke_direct + 324
12  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
13  libdispatch.dylib                   0x000000011b3cd565 _dispatch_block_invoke_direct + 324
14  libdispatch.dylib                   0x000000011b3cd401 dispatch_block_perform + 109
15  Foundation                          0x000000011127ece4 __NSOQSchedule_f + 342
16  libdispatch.dylib                   0x000000011b3c82b5 _dispatch_client_callout + 8
17  libdispatch.dylib                   0x000000011b3ce232 _dispatch_continuation_pop + 500
18  libdispatch.dylib                   0x000000011b3cca93 _dispatch_async_redirect_invoke + 760
19  libdispatch.dylib                   0x000000011b3d2a72 _dispatch_root_queue_drain + 568
20  libdispatch.dylib                   0x000000011b3d27dc _dispatch_worker_thread3 + 119
21  libsystem_pthread.dylib             0x000000011b8835a2 _pthread_wqthread + 1299
22  libsystem_pthread.dylib             0x000000011b88307d start_wqthread + 13
SmithKevin commented 7 years ago

Thanks for the headsup. The new Main Thread Checker feature in XCode 9 flags this method as not able to run in background, even though previous documentation did not. Up until this bug our tests showed it to still be working fine even in the background. We will get a fix out for this as well as a few other Xcode 9 warnings as soon as possible.

SmithKevin commented 7 years ago

Just some additional info. If this is preventing you from building, there is always the option to turn off the Main Thread Checker in Xcode. While not ideal, it should serve as a short term workaround.

To turn off the check open the "Product" menu item, select Schemes->Edit Scheme. Select the Diagnostic tab and uncheck the "Main Thread Checker" option. You might have to do this for both Run and Test schemes.

kirsplatrick commented 7 years ago

Thanks for the quick responses. We turned the Main Thread Checker off for tests since we were crashing there, but I believe the app itself is running ok, just with the runtime warning.

SmithKevin commented 7 years ago

@kirsplatrick I have pushed a new version of the SDK. Please let me know if that fixes your issues. It seems to work fine in all our tests.

kirsplatrick commented 7 years ago

@SmithKevin This fixed our issue. Thanks for the quick work. 👍