Google Analytics 锁定 iOS 应用程序中的主线程

Google Analytics Locks Main Thread in iOS App

我对 GA 有一个奇怪的问题。当我启动我的应用程序时,一切正常,但从另一个应用程序(App Store、Testflight 或测试启动器)启动它会导致死锁。 GA 阻止主线程处理 UIApplicationDidBecomeActiveNotification 通知,堆栈跟踪如下:

0   libsystem_kernel.dylib          0x000000018d255e1c __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x000000018d31a9d0 _pthread_cond_wait + 640
2   Foundation                      0x000000018ecc1620 -[NSCondition wait] + 240
3   Foundation                      0x000000018ec84a9c -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 792
4   Foundation                      0x000000018ec84764 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:] + 128
5   Search                          0x00000001005d457c -[GAIDataStore performBlockAndWait:withError:] + 260
6   Search                          0x00000001005d69f4 -[GAIDataStore save:] + 92
7   CoreFoundation                  0x000000018e22222c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
8   CoreFoundation                  0x000000018e221930 _CFXRegistrationPost + 400
9   CoreFoundation                  0x000000018e2216ac ___CFXNotificationPost_block_invoke + 60
10  CoreFoundation                  0x000000018e290b9c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1504
11  CoreFoundation                  0x000000018e163bf4 _CFXNotificationPost + 376
12  Foundation                      0x000000018ec6a6bc -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
13  UIKit                           0x00000001941322b0 -[UIApplication _deactivateForReason:notify:] + 828
14  UIKit                           0x0000000194350470 __61-[UIApplication _sceneSettingsPreLifecycleEventDiffInspector]_block_invoke + 124
[shortened]

这只会在每次从 Testflight 启动时发生。从 Springboard 打开应用程序工作正常。

我在 iPad Air 上打开应用程序时遇到了类似的问题,如滑过/拆分视图应用程序。我可以通过延迟初始化 GA 来解决问题。这是我的初始化代码:

GAI *theGAI = [GAI sharedInstance];

[theGAI trackerWithTrackingId:kTrackingId];
theGAI.trackUncaughtExceptions = YES;
#ifdef DEBUG
    theGAI.logger.logLevel = kGAILogLevelVerbose;
#endif

如何避免这种僵局?

我可以通过将屏幕跟踪放入后台线程来避免冻结。延迟第一次跟踪以避免与GAI的初始化代码冲突。

- (void)trackScreen {
    static NSTimeInterval sDelay = 0.75;
    NSString *theName = NSStringFromClass([self class]);
    id<GAITracker> theTracker = self.tracker;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [theTracker set:kGAIScreenName value:theName];
        [theTracker send:[[GAIDictionaryBuilder createScreenView] build]];
        sDelay = 1.0 / 16.0;
    });
}