Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This solution works</strong></p> <p>Note that this solution only works if myController is accessed on a background thread the first time. It will deadlock if called on the main thread.</p> <p>You want to use gcd. The key is serialize the creation of the object, so that regardless of the threads starting the block, it will always only be created exactly once.</p> <pre><code>- (UIViewController *)myController if (_myController == nil) { dispatch_sync(dispatch_get_main_queue(), ^ { if (_myController == nil) _myController = [[MyViewController alloc] init]; }); } return _myController; } </code></pre> <p>Here, even if multiple threads execute the block, the execution of the block is serialized onto the main thread and only one MyViewController can ever be created.</p> <p>You won't see a performance hit here unless the object is nil. </p> <p>Since the property is implicitly atomic, that means that in the setter the value will be autoreleased. This should make it suitable for mingling with your custom getting, since it will autorelease any value changes to _myController.</p> <p><a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW2" rel="nofollow noreferrer">http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW2</a></p> <p>However, you still may get into a race condition where you are setting the value on one thread but accessing it on another. Any time you set the value, you probably want to make sure and do something like this:</p> <p>dispatch_sync(dispatch_get_main_queue(), ^ { self.myController = {newValueOrNil} });</p> <p>This will make sure to serialize your setter methods calls without having to reinvent the wheel for atomic setters, which is very hard to get right.</p> <p><strong>This solution does not work</strong></p> <p>You want to use gcd. </p> <p><a href="http://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/func/dispatch_once" rel="nofollow noreferrer">http://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/func/dispatch_once</a></p> <p>See this post about singletons. I know you don't want a singleton, but this demonstrates how to use the method. You can easily adapt it. </p> <p><a href="https://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c">Create singleton using GCD&#39;s dispatch_once in Objective C</a></p>
    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