<?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: Blocks Rock &#8211; A Cocoa Asynchronous NSURLConnection block example</title>
	<atom:link href="http://blog.logichigh.com/2010/09/12/cocoa-blocks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/</link>
	<description>Logic High Software Blog</description>
	<lastBuildDate>Thu, 04 Apr 2013 09:59:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: 0xSina</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-1257</link>
		<dc:creator>0xSina</dc:creator>
		<pubDate>Tue, 17 Apr 2012 22:32:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-1257</guid>
		<description><![CDATA[As Marcelo Cantos stated, much better solution is to wrap this in NSURLConnections&#039; asynchronous model. YOu get more functionality, and speed. i wrote URLConnection, it&#039;s easy to use and add to your project. Find the link to it below:

http://messagesenttodeallocatedinstance.wordpress.com/2012/04/10/nsurlconnection-with-blocks/]]></description>
		<content:encoded><![CDATA[<p>As Marcelo Cantos stated, much better solution is to wrap this in NSURLConnections&#8217; asynchronous model. YOu get more functionality, and speed. i wrote URLConnection, it&#8217;s easy to use and add to your project. Find the link to it below:</p>
<p><a href="http://messagesenttodeallocatedinstance.wordpress.com/2012/04/10/nsurlconnection-with-blocks/" rel="nofollow">http://messagesenttodeallocatedinstance.wordpress.com/2012/04/10/nsurlconnection-with-blocks/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcelo Cantos</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-1230</link>
		<dc:creator>Marcelo Cantos</dc:creator>
		<pubDate>Thu, 09 Feb 2012 01:58:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-1230</guid>
		<description><![CDATA[The better solution would have been to wrap NSURLConnection&#039;s asynchronous model in blocks.]]></description>
		<content:encoded><![CDATA[<p>The better solution would have been to wrap NSURLConnection&#8217;s asynchronous model in blocks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 0x8badf00d</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-1226</link>
		<dc:creator>0x8badf00d</dc:creator>
		<pubDate>Fri, 03 Feb 2012 17:46:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-1226</guid>
		<description><![CDATA[Misleading title. Making a Synchronous network call on background thread doesn&#039;t make it asynchronous. You are blocking thread that makes synchronous request and waits for response, if the server takes several minutes to reply back your thread is blocked until then.]]></description>
		<content:encoded><![CDATA[<p>Misleading title. Making a Synchronous network call on background thread doesn&#8217;t make it asynchronous. You are blocking thread that makes synchronous request and waits for response, if the server takes several minutes to reply back your thread is blocked until then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julius</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-1203</link>
		<dc:creator>Julius</dc:creator>
		<pubDate>Thu, 29 Dec 2011 07:49:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-1203</guid>
		<description><![CDATA[seems like your async call cannot be cancel]]></description>
		<content:encoded><![CDATA[<p>seems like your async call cannot be cancel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BadPirate</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-929</link>
		<dc:creator>BadPirate</dc:creator>
		<pubDate>Sat, 25 Jun 2011 00:40:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-929</guid>
		<description><![CDATA[@Remy - Even better now... Using ARC (Auto retain / release, see XCode 4.2 documentation) the copy / autorelease nonsense is no longer required at all! (Celebration!)]]></description>
		<content:encoded><![CDATA[<p>@Remy &#8211; Even better now&#8230; Using ARC (Auto retain / release, see XCode 4.2 documentation) the copy / autorelease nonsense is no longer required at all! (Celebration!)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remy Demarest</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-920</link>
		<dc:creator>Remy Demarest</dc:creator>
		<pubDate>Sat, 04 Jun 2011 23:11:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-920</guid>
		<description><![CDATA[@Kevin Lohman: actually the blocks should be copied in only one case (but that&#039;s a big case): if the block is supposed to be live beyond the scope  in which it&#039;s created (it&#039;s the case in asynchronous operations like here), the block MUST be copied to the heap or you will get a crash for sure. And the code in this blog post is bound to crash.

Here is the corrections, it&#039;s at the dictionary creation:
[NSDictionary dictionaryWithObjectsAndKeys:  
                                       request,@&quot;request&quot;,  
                                       [[successBlock_ copy] autorelease],@&quot;success&quot;,  
                                       [[failureBlock_ copy] autorelease],@&quot;failure&quot;,  
                                       nil]];

Short explanation: blocks are allocated on the stack for optimization purpose, when the function returns the stack is cleaned up and the block dies out. The block has to be moved to the heap to exist beyond the scope, at that point it works like any other ObjC objects.

When the block is still on the stack, -retain doesn&#039;t do anything to the object, -retain must always return the receiver, that&#039;s part of the semantic, thus sending retain to a block (like what NSDictionary is doing) won&#039;t copy it to the heap. It has to be copied explicitly, you can then autorelease it because the dictionary will retain the block, in this case it works properly because -retain on a heap block increase its retain count just like normal ObjC objects.]]></description>
		<content:encoded><![CDATA[<p>@Kevin Lohman: actually the blocks should be copied in only one case (but that&#8217;s a big case): if the block is supposed to be live beyond the scope  in which it&#8217;s created (it&#8217;s the case in asynchronous operations like here), the block MUST be copied to the heap or you will get a crash for sure. And the code in this blog post is bound to crash.</p>
<p>Here is the corrections, it&#8217;s at the dictionary creation:<br />
[NSDictionary dictionaryWithObjectsAndKeys:<br />
                                       request,@"request",<br />
                                       [[successBlock_ copy] autorelease],@&#8221;success&#8221;,<br />
                                       [[failureBlock_ copy] autorelease],@&#8221;failure&#8221;,<br />
                                       nil]];</p>
<p>Short explanation: blocks are allocated on the stack for optimization purpose, when the function returns the stack is cleaned up and the block dies out. The block has to be moved to the heap to exist beyond the scope, at that point it works like any other ObjC objects.</p>
<p>When the block is still on the stack, -retain doesn&#8217;t do anything to the object, -retain must always return the receiver, that&#8217;s part of the semantic, thus sending retain to a block (like what NSDictionary is doing) won&#8217;t copy it to the heap. It has to be copied explicitly, you can then autorelease it because the dictionary will retain the block, in this case it works properly because -retain on a heap block increase its retain count just like normal ObjC objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Bradley</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-804</link>
		<dc:creator>Tom Bradley</dc:creator>
		<pubDate>Sat, 29 Jan 2011 13:36:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-804</guid>
		<description><![CDATA[Hi, this is a great idea! It tidies up the code so much!

However, I&#039;ve tidied it up a bit further. Instead of creating an NSThread to do the work just use Grand Central Dispatch. The method below is all you need.

+ (void)asyncRequest:(NSURLRequest *)request success:(void(^)(NSData *,NSURLResponse *))successBlock_ failure:(void(^)(NSData *,NSError *))failureBlock_
{
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		NSURLResponse *response = nil;
		NSError *error = nil;
		NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];

		if (error) {
			failureBlock_(data,error);
		} else {
			successBlock_(data,response);
		}
		
		[pool release];		
	});
}]]></description>
		<content:encoded><![CDATA[<p>Hi, this is a great idea! It tidies up the code so much!</p>
<p>However, I&#8217;ve tidied it up a bit further. Instead of creating an NSThread to do the work just use Grand Central Dispatch. The method below is all you need.</p>
<p>+ (void)asyncRequest:(NSURLRequest *)request success:(void(^)(NSData *,NSURLResponse *))successBlock_ failure:(void(^)(NSData *,NSError *))failureBlock_<br />
{<br />
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{</p>
<p>		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br />
		NSURLResponse *response = nil;<br />
		NSError *error = nil;<br />
		NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];</p>
<p>		if (error) {<br />
			failureBlock_(data,error);<br />
		} else {<br />
			successBlock_(data,response);<br />
		}</p>
<p>		[pool release];<br />
	});<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans Pinckaers</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-725</link>
		<dc:creator>Hans Pinckaers</dc:creator>
		<pubDate>Sun, 07 Nov 2010 19:56:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-725</guid>
		<description><![CDATA[Awesome thanks! You could also use GCD.]]></description>
		<content:encoded><![CDATA[<p>Awesome thanks! You could also use GCD.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Lohman</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-631</link>
		<dc:creator>Kevin Lohman</dc:creator>
		<pubDate>Wed, 22 Sep 2010 04:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-631</guid>
		<description><![CDATA[Okay... additionally, I&#039;ve found it&#039;s important to copy your blocks.  parameterBlock = [[parameterBlock copy] autorelease];
Especially if you plan on using multiple nested blocks.]]></description>
		<content:encoded><![CDATA[<p>Okay&#8230; additionally, I&#8217;ve found it&#8217;s important to copy your blocks.  parameterBlock = [[parameterBlock copy] autorelease];<br />
Especially if you plan on using multiple nested blocks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Lohman</title>
		<link>http://blog.logichigh.com/2010/09/12/cocoa-blocks/comment-page-1/#comment-628</link>
		<dc:creator>Kevin Lohman</dc:creator>
		<pubDate>Tue, 21 Sep 2010 02:04:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.logichigh.com/?p=237#comment-628</guid>
		<description><![CDATA[I should comment... seems blocks are a little more work then I&#039;d originally hoped.  While this will work in some places, NSThread is a lot of overhead, and using a Singleton connection manager is probably a more efficient way to go.  Additionally, when you are using values in blocks, it&#039;s important to make sure they are in scope (make copies of parameter variables) and to check for the existence of those variables before calling them.  Otherwise nasty, hard to track errors can occur with nested blocks.]]></description>
		<content:encoded><![CDATA[<p>I should comment&#8230; seems blocks are a little more work then I&#8217;d originally hoped.  While this will work in some places, NSThread is a lot of overhead, and using a Singleton connection manager is probably a more efficient way to go.  Additionally, when you are using values in blocks, it&#8217;s important to make sure they are in scope (make copies of parameter variables) and to check for the existence of those variables before calling them.  Otherwise nasty, hard to track errors can occur with nested blocks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
