title: Using dispatch_once_t per object and not per class
body: <p>There are multiple sources calling a particular method, but I would like to ensure that it is called exactly once (per object)</p>
  
  <p>I would like to use syntax like</p>
  
  <pre><code>// method called possibly from multiple places (threads)
  -(void)finish
  {
  
      static dispatch_once_t onceToken;
      dispatch_once(&amp;onceToken, ^{
          [self _finishOnce]; // should happen once per object
      });
  }
  // should only happen once per object
  -(void)_finishOnce{...}
  </code></pre>
  
  <p>Problem is the token is shared accross all instances of the same class - so not a good solution - is there a dispatch_once_t per object - if not what is the best way to ensure it is called once?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is a proposed solution I am thinking of - does it seem alright?</p>
  
  <pre><code>@interface MyClass;
  
  @property (nonatomic,strong) dispatch_queue_t dispatchOnceSerialQueue; // a serial queue for ordering of query to a ivar
  
  @property (nonatomic) BOOL didRunExactlyOnceToken;
  
  @end
  
  @implementation MyClass
  
  -(void)runExactlyOnceMethod
  {
    __block BOOL didAlreadyRun = NO;
    dispatch_sync(self.dispatchOnceSerialQueue, ^{
       didAlreadyRun = _didRunExactlyOnceToken;
       if (_didRunExactlyOnceToken == NO) {
          _didRunExactlyOnceToken = YES;
       }
    });
    if (didAlreadyRun == YES)
    {
      return;
    }
    // do some work once
  }
  </code></pre>