@@ -501,34 +501,41 @@ -(BOOL) idle
501501 BOOL retval = NO;
502502 //we are idle when we are not connected (and not trying to)
503503 //or: the catchup is done, no unacked stanzas are left in the smacks queue and receive and send queues are empty (no pending operations)
504- unsigned long unackedCount = 0 ;
505- @synchronized (_stateLockObject) {
506- unackedCount = (unsigned long )[self .unAckedStanzas count ];
507- }
504+ id unackedCount = @"unchecked";
508505 if(
509506 (
510507 //test if this account was permanently logged out but still has stanzas pending (this can happen if we have no connectivity for example)
511508 //--> we are not idle in this case because we still have pending outgoing stanzas
512509 _accountState<kStateReconnecting &&
513- !_reconnectInProgress &&
514- !unackedCount
510+ !_reconnectInProgress
515511 ) || (
516512 //test if we are connected and idle (e.g. we're done with catchup and neither process any incoming stanzas nor trying to send anything)
517513 _catchupDone &&
518- !unackedCount &&
519514 ![_parseQueue operationCount] && //if something blocks the parse queue it is either an incoming stanza currently processed or waiting to be processed
520515 //[_receiveQueue operationCount] <= ([NSOperationQueue currentQueue]==_receiveQueue ? 1 : 0) &&
521516 ![_sendQueue operationCount] &&
522517 ![_inCatchup count]
523518 )
524519 )
525520 retval = YES;
521+ //only check unacked count if needed (this makes sure we don't hold the state lock unnecessarily and block the main thread)
522+ if(retval)
523+ {
524+ @synchronized(_stateLockObject) {
525+ NSUInteger unacked = [self.unAckedStanzas count];
526+ if(unacked)
527+ {
528+ retval = NO;
529+ unackedCount = @(unacked);
530+ }
531+ }
532+ }
526533 _lastIdleState = retval;
527534 DDLogVerbose(@("%@ --> Idle check:\n"
528535 "\t_accountState < kStateReconnecting = %@\n"
529536 "\t_reconnectInProgress = %@\n"
530537 "\t_catchupDone = %@\n"
531- " \t [self.unAckedStanzas count] = %lu \n "
538+ "\t[self.unAckedStanzas count] = %@ \n"
532539 "\t[_parseQueue operationCount] = %lu\n"
533540 //"\t[_receiveQueue operationCount] = %lu\n"
534541 "\t[_sendQueue operationCount] = %lu\n"
0 commit comments