简体   繁体   English

超级中的dispatch_once?

[英]dispatch_once in super class?

If I call dispatch_once inside an instance method of a super class, will it execute once for each subclass? 如果我在超类的实例方法中调用dispatch_once ,它会为每个子类执行一次吗? I'm guessing not because the Xcode Grand Central Dispatch (GCD) Reference says it "executes a block object once and only once for the lifetime of an application." 我猜不是因为Xcode Grand Central Dispatch(GCD)参考说它“在应用程序的生命周期内执行一次且只执行一次一次。”

Your reading of the documentation is correct. 您阅读文档是正确的。 That block will execute only once, provided you are using the same once token. 只要您使用相同的一次令牌,该块将仅执行一次。

Your reading of the documentation is almost correct: dispatch_once() will execute only once for the lifetime of a given dispatch_once_t variable. 您对文档的阅读几乎是正确的:dispatch_once()将仅在给定dispatch_once_t变量的生命周期内执行一次。 Since most applications declare this variable as a static global, the documentation covers this pattern. 由于大多数应用程序将此变量声明为静态全局变量,因此文档涵盖了此模式。 If you have a token which is unique for each class instance and starts out properly zero-initialized, then it will execute for each instance. 如果你有一个对每个类实例都是唯一的标记,并且正确地启动了零初始化,那么它将为每个实例执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM