`-applicationDidBecomeActive` 怎么可能同时在两个独立的后台线程上分派?

How could it be possible for `-applicationDidBecomeActive` to be dispatched on two separate background threads simultaneously?

我们发生了一次奇怪的崩溃——现在我确定是什么原因造成的,但我注意到一件奇怪的事情是所有崩溃日志在两个单独的后台线程上都有 -applicationDidBecomeActive

Thread 5:
0   libsystem_kernel.dylib               0x00007fffaebac456 semaphore_wait_trap + 10
1   MyApp                               0x0000000107389da1 -[OutputManager(TechSmithCloud) reloadCloudDestinations] (OutputManager+TechSmithCloud.m:59)
2   MyApp                               0x000000010728c4b4 __44-[AppController applicationDidBecomeActive:]_block_invoke (AppController.m:696)
3   libdispatch.dylib                    0x00007fffaea57f5f _dispatch_call_block_and_release + 12
4   libdispatch.dylib                    0x00007fffaea4f128 _dispatch_client_callout + 8
5   libdispatch.dylib                    0x00007fffaea51099 _dispatch_root_queue_drain + 917
6   libdispatch.dylib                    0x00007fffaea50cb7 _dispatch_worker_thread3 + 99
7   libsystem_pthread.dylib              0x00007fffaec9b746 _pthread_wqthread + 1299
8   libsystem_pthread.dylib              0x00007fffaec9b221 start_wqthread + 13


Thread 13:
0   libsystem_kernel.dylib               0x00007fffaebac456 semaphore_wait_trap + 10
1   MyApp                               0x0000000107389da1 -[OutputManager(TechSmithCloud) reloadCloudDestinations] (OutputManager+TechSmithCloud.m:59)
2   MyApp                               0x000000010728c4b4 __44-[AppController applicationDidBecomeActive:]_block_invoke (AppController.m:696)
3   libdispatch.dylib                    0x00007fffaea57f5f _dispatch_call_block_and_release + 12
4   libdispatch.dylib                    0x00007fffaea4f128 _dispatch_client_callout + 8
5   libdispatch.dylib                    0x00007fffaea51099 _dispatch_root_queue_drain + 917
6   libdispatch.dylib                    0x00007fffaea50cb7 _dispatch_worker_thread3 + 99
7   libsystem_pthread.dylib              0x00007fffaec9b746 _pthread_wqthread + 1299
8   libsystem_pthread.dylib              0x00007fffaec9b221 start_wqthread + 13

而且我无法复制它(我在 -applicationDidBecomeActive 中注销了一条语句,它一次只注销一次)

所以我不确定这怎么可能,或者这是否是一个实际问题

也许是信号量相关的东西?

代码如下:

- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
                  {
                     [[OutputManager sharedOutputManager] reloadCloudDestinations];
                  });

}

编辑: -reloadCloudDestinations

代码
-(void) reloadCloudDestinations
{
   [self setupCloudLibrary];
   TSCAccount* account = [TSCCloudServices activeAccount];
   if( account.status == TSCAccount_SignedIn )
   {
      dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
      __weak __typeof(self) weakSelf = self;

      @try
      {
         AFHTTPSessionManager *test = (AFHTTPSessionManager*)[(TSCAccountHTTPSession*)[self.libraryCore valueForKey:@"sessionManager"] valueForKey:@"httpClient"];
         NSLog(@"%@", test);
         [self.libraryCore destinationsWithActions:NEVER_TRANSLATE(@"publish,list") completionBlock:^(NSArray *destinations, NSError *error) {

            @try
            {
               if( error == nil )
               {
                  [weakSelf createButtonsForNewDestinationsNotAlreadyPresent:destinations];
               }
               else
               {
                  NSLog(@"destinationsWithBlock error: %@", error);
               }
            }
            @catch (NSException* exception)
            {
               NSLog(@"reloadCloudDestinations - destinationWithBlock completion - An exception was thrown %@", exception);
            }
            @finally
            {
              dispatch_semaphore_signal(semaphore);
            }
         }];

         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
      }
      @catch (NSException* exception)
      {
         NSLog(@"reloadCloudDestinations - An exception was thrown %@", exception);
         dispatch_semaphore_signal(semaphore);
      }
   }
}

回溯的符号为 __44-[AppController applicationDidBecomeActive:]_block_invoke。那不是 -applicationDidBecomeActive: 方法本身。它是编译器为您的 -applicationDidBecomeActive: 方法中出现的块生成的函数的名称。

块函数出现在后台线程中,因为您正在将它分派到主队列以外的队列。这不是问题,只是一个解释。

它出现多次可能是因为您的应用在其整个生命周期内变得活跃、退出活跃、然后再次活跃。每次它激活时,-applicationDidBecomeActive: 都会在主线程上被调用。这会将块提交到全局并发队列,并且假设有可用的 CPU 核心和其他系统资源,然后它将在后台线程上执行。

一个悬而未决的问题是为什么当您的崩溃发生时块仍然 运行 而不是在短期内完成。您没有显示 -[OutputManager reloadCloudDestinations] 的代码,所以很难知道。显然,它等待一个计数恰好为零或更小的信号量。