iPhone - Grand Central Dispatch在iPhone 4S上无法正常工作

时间:2011-11-18 17:56:07

标签: iphone ios cocoa-touch ipad grand-central-dispatch

我有这个应用程序是我的iPhone 4启动时创建的。现在,这个应用程序没有在iPhone 4S上运行。

我已将罪魁祸首部分确定为GCD部分。这是:

dispatch_group_t my_group = dispatch_group_create();

dispatch_queue_t queue1 = 
        dispatch_queue_create("Queue 1", NULL);

dispatch_queue_t queue2 = 
        dispatch_queue_create("Queue 2", NULL);


dispatch_group_async(my_group, queue1, ^{
        [self doStuff1];
});

dispatch_group_async(my_group, queue2, ^{
        [self doStuff2];
});

dispatch_group_notify(my_group, dispatch_get_main_queue(), ^{
 // this is block 3, this is to be executed after both queues end processing
 // this is never executed on iPhone 4S, but is executed on iPhone4
 // no error message, but execution never starts inside this block
});

这个想法是这样的:创建了两个队列和一个组。我使用该组以异步方式为两个队列启动任务。两者完成后,该组将触发另一个任务块。

这项工作非常适用于iPhone 4,但最终的第3块永远不会到达。

有什么理由吗?有线索吗?

感谢。

2 个答案:

答案 0 :(得分:5)

也许doStuff1doStuff2处于死锁状态,或其他阻塞主线程的东西? 4S有多个内核,与4不同,因此可能会遇到一些您以前从未见过的多线程锁定问题。

您确定两个块实际上都在完成,并且主线程可用于运行生成的块吗?也许一些完整的代码(即doStuff1和2的主体)会有帮助吗?

答案 1 :(得分:2)

我要做的第一件事是将代码更改为dispatch_async queue1上的所有内容;显然,你不会得到并发性,但如果问题立刻消失,它会立即知道dostuff1和dostuff2是否会以某种方式发生冲突。在单个核心机器上,这可能是您之前实际看到的执行行为。