小编Sun*_*ist的帖子

块和ViewController线程安全

我一直在关注Game Center代码示例,GKTapper,以及开发人员对其实现的评论对我来说没有多大意义的一个部分.代码插入下面.我不明白为什么调度一个修改主线程上的viewcontroller的块不安全?

他提到"如果在一个在二级队列上执行的块中引用了viewcontroller,那么它可能会在主队列之外被释放.即使在主线程上调度了实际块,也是如此." 如果处理发布的代码在主UI线程上(在主runloop上),那怎么可能呢?或者我有没有得到Block/GCD的东西?

更让我感到好奇的是他的解决方案是如何解决这个问题的."因为"callDelegate"是访问委托的唯一方法,我可以确保委托在任何块回调中都不可见." (Delegate是一个viewcontroller在这里)

有人可以告诉我这件事吗?我对块和GCD很新,所以也许我错过了一些简单的东西......

// NOTE:  GameCenter does not guarantee that callback blocks will be execute on the main thread.
// As such, your application needs to be very careful in how it handles references to view
// controllers.  If a view controller is referenced in a block that executes on a secondary queue,
// that view controller may be released (and dealloc'd) outside the main queue.  This is true
// even if the actual …
Run Code Online (Sandbox Code Playgroud)

iphone gamekit grand-central-dispatch objective-c-blocks game-center

2
推荐指数
1
解决办法
1836
查看次数