<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Singletons in Cocoa/Objective-C</title>
	<atom:link href="http://eschatologist.net/blog/?feed=rss2&#038;p=178" rel="self" type="application/rss+xml" />
	<link>http://eschatologist.net/blog/?p=178</link>
	<description>Ask me how it ends…</description>
	<lastBuildDate>Sat, 29 Dec 2012 12:18:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Carlo</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-24403</link>
		<dc:creator>Carlo</dc:creator>
		<pubDate>Fri, 16 Dec 2011 01:14:01 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-24403</guid>
		<description><![CDATA[&lt;p&gt;This is very very interesting. I&#039;m in the situation that I want to create a global &quot;manager&quot; to manage Core Data (all the methods to access Core Data in the delegate make me really uncomfortable).
Now I&#039;m wondering: what is the right thing to do if singleton is bad?&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>This is very very interesting. I&#8217;m in the situation that I want to create a global &#8220;manager&#8221; to manage Core Data (all the methods to access Core Data in the delegate make me really uncomfortable).
Now I&#8217;m wondering: what is the right thing to do if singleton is bad?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shea Ako</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-22593</link>
		<dc:creator>Shea Ako</dc:creator>
		<pubDate>Fri, 21 Oct 2011 17:18:25 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-22593</guid>
		<description><![CDATA[&lt;p&gt;addendum to my previous comment - I meant to say,&lt;/p&gt;

&lt;p&gt;I use the first method described in the article and put a lock around:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   if (sharedManager == nil) {...}
&lt;/code&gt;&lt;/pre&gt;
]]></description>
		<content:encoded><![CDATA[<p>addendum to my previous comment &#8211; I meant to say,</p>

<p>I use the first method described in the article and put a lock around:</p>

<pre><code>   if (sharedManager == nil) {...}
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shea Ako</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-22592</link>
		<dc:creator>Shea Ako</dc:creator>
		<pubDate>Fri, 21 Oct 2011 17:16:01 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-22592</guid>
		<description><![CDATA[&lt;p&gt;I&#039;ve run into a situation that demonstrates that the example published here is not actually thread safe.&lt;/p&gt;

&lt;p&gt;Two threads call +sharedManager at (more or less) the same time. The earlier call to +sharedManager triggers +initialize on its thread, but before +initialize completes the second thread executes +sharedManager.&lt;/p&gt;

&lt;p&gt;When +sharedManager is called on the second thread it can return nil if +initialize hasn&#039;t finished yet on the first thread.&lt;/p&gt;

&lt;p&gt;Using some sort of locking is necessary for thread safety.&lt;/p&gt;

&lt;p&gt;I use a lock in +sharedManager.around&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   if (sharedManager == nil) {...}
&lt;/code&gt;&lt;/pre&gt;
]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve run into a situation that demonstrates that the example published here is not actually thread safe.</p>

<p>Two threads call +sharedManager at (more or less) the same time. The earlier call to +sharedManager triggers +initialize on its thread, but before +initialize completes the second thread executes +sharedManager.</p>

<p>When +sharedManager is called on the second thread it can return nil if +initialize hasn&#8217;t finished yet on the first thread.</p>

<p>Using some sort of locking is necessary for thread safety.</p>

<p>I use a lock in +sharedManager.around</p>

<pre><code>   if (sharedManager == nil) {...}
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Parnot</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-20356</link>
		<dc:creator>Charles Parnot</dc:creator>
		<pubDate>Fri, 19 Aug 2011 22:08:31 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-20356</guid>
		<description><![CDATA[&lt;p&gt;Is this thread-safe?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@implementation Foo

static id sharedInstance;

+(id)sharedInstance
{
    if (sharedInstance)
        return sharedInstance;

    @synchronized(self)
    {
        if (!sharedInstance)
            sharedInstance = [[self alloc] init];
    }
    return sharedInstance;
}

@end
&lt;/code&gt;&lt;/pre&gt;
]]></description>
		<content:encoded><![CDATA[<p>Is this thread-safe?</p>

<pre><code>@implementation Foo

static id sharedInstance;

+(id)sharedInstance
{
    if (sharedInstance)
        return sharedInstance;

    @synchronized(self)
    {
        if (!sharedInstance)
            sharedInstance = [[self alloc] init];
    }
    return sharedInstance;
}

@end
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Otto Schnurr</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-12471</link>
		<dc:creator>Otto Schnurr</dc:creator>
		<pubDate>Tue, 28 Sep 2010 18:54:51 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-12471</guid>
		<description><![CDATA[&lt;p&gt;@scott: When a static variable is declared inside a function, it&#039;s only initialized the first time the function is executed.&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>@scott: When a static variable is declared inside a function, it&#8217;s only initialized the first time the function is executed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fred McCann</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-6744</link>
		<dc:creator>Fred McCann</dc:creator>
		<pubDate>Fri, 21 May 2010 22:40:07 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-6744</guid>
		<description><![CDATA[&lt;p&gt;I&#039;ve written up an in-depth discussion of the singleton pattern here:&lt;/p&gt;

&lt;p&gt;http://www.duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve written up an in-depth discussion of the singleton pattern here:</p>

<p><a href="http://www.duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/" rel="nofollow">http://www.duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bangnoise</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-3761</link>
		<dc:creator>bangnoise</dc:creator>
		<pubDate>Mon, 01 Feb 2010 11:32:28 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-3761</guid>
		<description><![CDATA[&lt;p&gt;But your singleton isn&#039;t a singleton at all - you can [[SomeManager alloc] init] as many instances as you like. The point of the Apple way which overrides allocWithZone: is to enforce singleton behaviour - if that&#039;s not important to a singleton class, it maybe doesn&#039;t need to be a singleton anyway.&lt;/p&gt;

&lt;p&gt;@synchronized([SomeManager class]) {
}&lt;/p&gt;

&lt;p&gt;Is an improvement on Apple&#039;s example locking, I think..?&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>But your singleton isn&#8217;t a singleton at all &#8211; you can [[SomeManager alloc] init] as many instances as you like. The point of the Apple way which overrides allocWithZone: is to enforce singleton behaviour &#8211; if that&#8217;s not important to a singleton class, it maybe doesn&#8217;t need to be a singleton anyway.</p>

<p>@synchronized([SomeManager class]) {
}</p>

<p>Is an improvement on Apple&#8217;s example locking, I think..?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dustin L. Howett</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-3688</link>
		<dc:creator>Dustin L. Howett</dc:creator>
		<pubDate>Sat, 23 Jan 2010 20:46:52 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-3688</guid>
		<description><![CDATA[&lt;p&gt;Amending my last comment: &quot;Except for the case in which a subclass of yours does not provide +initialize.&quot;&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>Amending my last comment: &#8220;Except for the case in which a subclass of yours does not provide +initialize.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dustin L. Howett</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-3687</link>
		<dc:creator>Dustin L. Howett</dc:creator>
		<pubDate>Sat, 23 Jan 2010 20:35:01 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-3687</guid>
		<description><![CDATA[&lt;p&gt;There&#039;s actually no way for +initialize to be called with a self that isn&#039;t the class +initialize belongs to, by the runtime. The runtime calls initialize in reverse order for the entire superclass tree (assuming that class has not already been initialized) and does not call &quot;[super initialize].&quot; +initialize is called once and only once for every class, as noted in the Apple documentation.
The if(self == [SomeManager class]) guard is therefore unnecessary.&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>There&#8217;s actually no way for +initialize to be called with a self that isn&#8217;t the class +initialize belongs to, by the runtime. The runtime calls initialize in reverse order for the entire superclass tree (assuming that class has not already been initialized) and does not call &#8220;[super initialize].&#8221; +initialize is called once and only once for every class, as noted in the Apple documentation.
The if(self == [SomeManager class]) guard is therefore unnecessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kaelin</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-1624</link>
		<dc:creator>Kaelin</dc:creator>
		<pubDate>Wed, 07 Oct 2009 01:27:18 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-1624</guid>
		<description><![CDATA[&lt;p&gt;GCD now offers the possibility of doing this with dispatch_once(). This is almost certainly the most performant technique that&#039;s both thread-safe and concise:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(id)sharedInstance;
{
static id sharedInstance = nil;
static dispatch_once_t once = 0;
dispatch_once(&amp;once, ^ {
    sharedInstance = [[self alloc] init];
});
return sharedInstance;
}&lt;/li&gt;
&lt;/ul&gt;
]]></description>
		<content:encoded><![CDATA[<p>GCD now offers the possibility of doing this with dispatch_once(). This is almost certainly the most performant technique that&#8217;s both thread-safe and concise:</p>

<ul>
<li>(id)sharedInstance;
{
static id sharedInstance = nil;
static dispatch_once_t once = 0;
dispatch_once(&amp;once, ^ {
    sharedInstance = [[self alloc] init];
});
return sharedInstance;
}</li>
</ul>
]]></content:encoded>
	</item>
	<item>
		<title>By: scott lewis</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-1416</link>
		<dc:creator>scott lewis</dc:creator>
		<pubDate>Sun, 27 Sep 2009 11:53:42 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-1416</guid>
		<description><![CDATA[&lt;p&gt;correct me if I&#039;m wrong, but there seems to be a typo in the very first example as the static declaration is inside the class method, which means it will always be set to nil, and thus not really work as a &quot;somewhat&quot; singleton. Is that correct?&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>correct me if I&#8217;m wrong, but there seems to be a typo in the very first example as the static declaration is inside the class method, which means it will always be set to nil, and thus not really work as a &#8220;somewhat&#8221; singleton. Is that correct?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eschaton</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-234</link>
		<dc:creator>eschaton</dc:creator>
		<pubDate>Mon, 06 Jul 2009 21:20:13 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-234</guid>
		<description><![CDATA[&lt;p&gt;You really shouldn&#039;t need to be worried about performance of singleton objects.&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>You really shouldn&#8217;t need to be worried about performance of singleton objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mo Ayazifar</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-204</link>
		<dc:creator>Mo Ayazifar</dc:creator>
		<pubDate>Mon, 29 Jun 2009 21:22:47 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-204</guid>
		<description><![CDATA[&lt;p&gt;Then finally which one is gonna be the best one regarding to performance? and how are we supposed to use them form outside? is there any sample code available for that?&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>Then finally which one is gonna be the best one regarding to performance? and how are we supposed to use them form outside? is there any sample code available for that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eschaton</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-154</link>
		<dc:creator>eschaton</dc:creator>
		<pubDate>Thu, 18 Jun 2009 05:21:14 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-154</guid>
		<description><![CDATA[&lt;p&gt;Yeah; using &lt;code&gt;@synchronized (self)&lt;/code&gt; in a class method that could be invoked on a subclass is risky.&lt;/p&gt;

&lt;p&gt;I reformatted your code into Markdown, since it didn&#039;t survive the HTML editor widget thingy.&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>Yeah; using <code>@synchronized (self)</code> in a class method that could be invoked on a subclass is risky.</p>

<p>I reformatted your code into Markdown, since it didn&#8217;t survive the HTML editor widget thingy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clark Cox</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-153</link>
		<dc:creator>Clark Cox</dc:creator>
		<pubDate>Thu, 18 Jun 2009 05:13:40 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-153</guid>
		<description><![CDATA[&lt;p&gt;(Of course, that doesn&#039;t address the subclassing issue that you mentioned)&lt;/p&gt;
]]></description>
		<content:encoded><![CDATA[<p>(Of course, that doesn&#8217;t address the subclassing issue that you mentioned)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clark Cox</title>
		<link>http://eschatologist.net/blog/?p=178&#038;cpage=1#comment-152</link>
		<dc:creator>Clark Cox</dc:creator>
		<pubDate>Thu, 18 Jun 2009 05:07:05 +0000</pubDate>
		<guid isPermaLink="false">http://eschatologist.net/blog/?p=178#comment-152</guid>
		<description><![CDATA[&lt;p&gt;Another way to make a singleton, that is still lazily loaded, thread-safe and only incurs the cost of synchronization once (i.e. it&#039;s like a double-checked lock, except that it actually works):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@implementation Foo

static id sharedInstance;

+(id)sharedInstance {
    @synchronized(self) {
        if(!sharedInstance) {
            sharedInstance = [[self alloc] init];
            class_replaceMethod(object_getClass(self),
                @selector(sharedInstance),
                [self methodForSelector: @selector(sharedInstance2)],
                &quot;@@:&quot;);
        }
    }

    return sharedInstance;
}

+(id)sharedInstance2 {
    return sharedInstance;
}

@end
&lt;/code&gt;&lt;/pre&gt;
]]></description>
		<content:encoded><![CDATA[<p>Another way to make a singleton, that is still lazily loaded, thread-safe and only incurs the cost of synchronization once (i.e. it&#8217;s like a double-checked lock, except that it actually works):</p>

<pre><code>@implementation Foo

static id sharedInstance;

+(id)sharedInstance {
    @synchronized(self) {
        if(!sharedInstance) {
            sharedInstance = [[self alloc] init];
            class_replaceMethod(object_getClass(self),
                @selector(sharedInstance),
                [self methodForSelector: @selector(sharedInstance2)],
                "@@:");
        }
    }

    return sharedInstance;
}

+(id)sharedInstance2 {
    return sharedInstance;
}

@end
</code></pre>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.711 seconds -->
