0

Im using XMPPFramework and in it's code there's a method like this:

- (NSDictionary *)occupants
{
    if (dispatch_get_current_queue() == moduleQueue)
    {
        return occupants;
    }
    else
    {
        __block NSDictionary *result;

        dispatch_sync(moduleQueue, ^{//IT BLOCKS HERE, WITHOUT MESSAGE
            result = [occupants copy];
        });

        return [result autorelease];
    }
}

[EDIT] It blocks inconsistently, not always, since the app is not doing anything I pause it and I see the thread has stopped there, and it never continues to execute. What is wrong? Any ideas?

Thanks

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
subharb
  • 3,374
  • 8
  • 41
  • 72

2 Answers2

3

The behavior you explain perfectly matches with the one that appears when you try to send perform an operation on main thread via GCD while being on the main thread. So you should check if moduleQueue is the main queue, then this is it. Try checking if it is the main queue if it is, skip the dispatch_sync block.

Eugene
  • 10,006
  • 4
  • 37
  • 55
  • 1
    Well, even if the moduleQueue isn't the main queue, if the code were called from the main thread, the app would freeze until the work was done on the background thread. And it may be a deadlock where another thread is sync-ing to the main thread/queue within a block running on the moduleQueue. – Ken Thomases Apr 25 '12 at 17:33
  • The code already does not call `dispatch_sync` if the current queue is the queue that the method would dispatch to. More probably, the `moduleQueue` is not the main queue (or it is but the call is being made from some other queue); the queue is just really busy, or blocked on something else. – Peter Hosey Apr 26 '12 at 00:11
0

Blocks sometimes need to retain variables to ensure they are available when they execute. If you use a local variable inside a block, you should initialise it to zero where you declare it outside the block.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • 1
    Blocks don't implicitly retain the values of variables declared with `__block`. Also, if the questioner is using ARC, the variable is already implicitly initialized to `nil`. – Peter Hosey Apr 26 '12 at 00:06