Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I should note first that this isn't a good thing to do in any situation other than testing; even then, proceed with care -- AliSoftware provides some details and example code in comments below. See also the interesting answers at <a href="https://stackoverflow.com/q/13856037">Can I declare a <code>dispatch_once_t</code> predicate as a member variable instead of static?</a>, including some important information <a href="https://stackoverflow.com/a/19845164/">from the horse's mouth</a>.</p> <p><code>dispatch_once_t</code> is a <code>typedef</code>d <code>long</code>. Its false value is 0. If you reset that flag to 0, <code>dispatch_once()</code> will run again. Your problem is "just" how to change the value of a static variable from another compilation unit. For this, I think you need a debug/unit test hook, like so:</p> <p>MakeWhoopie.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; void makeWhoopie(void); #ifdef DEBUG void resetDispatchOnce(void); #endif </code></pre> <p>MakeWhoopie.m</p> <pre><code>#include "MakeWhoopie.h" static dispatch_once_t * once_token_debug; void makeWhoopie(void) { static dispatch_once_t once_token; once_token_debug = &amp;once_token; // Store address of once_token // to access it in debug function. dispatch_once(&amp;once_token, ^{ NSLog(@"That's what you get, folks."); }); NSLog(@"Making whoopie."); } #ifdef DEBUG void resetDispatchOnce(void) { *once_token_debug = 0; } #endif </code></pre> <p>(You could also move <code>once_token</code> up to file level and change it directly.)</p> <p>Trying this out:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "MakeWhoopie.h" int main(int argc, const char * argv[]) { @autoreleasepool { makeWhoopie(); makeWhoopie(); resetDispatchOnce(); makeWhoopie(); } return 0; } </code></pre> <p>Results in:</p> <blockquote> <p>2012-06-07 18:45:28.134 ResetDispatchOnce[8628:403] That's what you get, folks.<br> 2012-06-07 18:45:28.163 ResetDispatchOnce[8628:403] Making whoopie.<br> 2012-06-07 18:45:28.164 ResetDispatchOnce[8628:403] Making whoopie.<br> 2012-06-07 18:45:28.165 ResetDispatchOnce[8628:403] That's what you get, folks.<br> 2012-06-07 18:45:28.165 ResetDispatchOnce[8628:403] Making whoopie. </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload