App blocks while dipatching a queue

2019-01-29 08:03发布

问题:

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

回答1:

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.



回答2:

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.